JSON error in AFNetworking - ios

I'm trying to use the AFNetworking library to send a post request to an URL like this:
https://m.com/api/2.0/basic/sim_balance.json
That expects a username and password in the URL that are my parameters (as NSDictionary).
The URL gives a JSON back like this:
{
"valid_until": "2011-05-05 22:13:28",
"sms": 0,
"sms_super_on_net_max": 300,
"voice_super_on_net": 0,
"credits": "0.00",
"bundles": [],
"is_expired": true,
"sms_super_on_net": 0,
"voice_super_on_net_max": 3600,
"data": 0
}
This is my request function:
+(void) request:(NSString *)endpoint : (NSDictionary *) parameters
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:endpoint parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
I always get this as error:
Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x904b7e0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
I validated the JSON and the server is sending valid JSON.
Can someone help me out on this?

To get AFHTTPRequestOperationManager to serialize the request body as JSON, you need to set the value of its requestSerializer:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:....];
If you do not set the requestSerializer property, then the manager object will use x-www-form-urlencoded serialization by default.
Note however that AFHTTPRequestOperationManager will handle JSON in the response body by default.

I found the solution thanks to ester. Also you need to set the basic auth headers right.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:#"Tom" password:#"test"];
[manager GET:endpoint parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Response data: %#",operation.responseData);
NSLog(#"Response String: %#",operation.responseString);
NSLog(#"Error: %#", error);
}];
}

Related

Domain=NSCocoaErrorDomain Code=3840 error in afnetworking 2.0

i am unable to find solution of NSCocoaErrorDomain Code=3840 error in afnetworking 2.0
this is code i used
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:#"text/html"];
[manager POST:url parameters:inputs success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
this is the error getting
Error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
Try to use AFHTTPResponseSerializer instead
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
Because you're using acceptableContentType of #"text/html", I assume you don't need a JSON serializer on response call. So this should be fine for your case.

AFNetworking not receiving response data

I am using AFNetworking to GET request by posting json object. But i am receiving empty value. I am using the following code.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *params = # {#"email" :user_name, #"password" :pass_word };
[manager GET:HOST_URL parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
NSDictionary *weather = (NSDictionary *)responseObject;
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
NSLog(#"opstr%#",operation.responseString);
}];
What method i should use. If i use POST, i am getting error saying that "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.)
When i use GET i am getting response as empty. I am not sure where i am wrong.
Thanks in advance.
It means that your request response is not a valid json. You can correct the web service to return proper json. Or remove the following line
manager.requestSerializer = [AFJSONRequestSerializer serializer];
because it is converting response to json.
To allow fragments, change the code to following
[manager GET:HOST_URL parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
id json = [NSJSONSerialization JSONObjectWithData:responseObject
options:NSJSONReadingAllowFragments
error:&deserializingError];
NSLog(#"JSON: %#", responseObject);
NSDictionary *weather = (NSDictionary *)responseObject;
}
failure:
^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
NSLog(#"opstr%#",operation.responseString);
}];

how to post parameter like {"register_id":"3"} in AFNetworking

i tried it but didn't work in AFNetworking only showing parameters error
but i used postman to check and when i send data via key and value it showing error but from raw data i send {"register_id":"3"}
then it will show me data so how to post parameter like this in AFNetworking.
using This Link
http://www.icubemedia.net/visitorbook/display_all.php
is any one can help me for that how to post that data
log error is:
2015-06-19 14:05:08.078 DemoAFNetworking[72771:1160924]
{"msg":"parameter missing!"}
Indeed there are no parameters missing, the fact that the request worked in Postman was the key. On the one hand, you should be trying to POST to that URL, not GET. On the other hand, since you are sending a JSON, you need the appropriate serializer.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//JSON Serializer
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = #{#"register_id": #"3"};
[manager POST:#"http://www.icubemedia.net/visitorbook/display_all.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Check this example on how to do a GET with simple parameter with AFNetworking 2.0:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = #{#"foo": #"bar"};
[manager GET:#"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
EDIT 1: added JSON serializer ;)

json data parameter changes structure upon reaching php server

I'm working on a mobile app that takes personal information from users then saves it to the php server. I'm having a problem on the data with array of dictionaries, how do I fix this?
The sample data that the mobile app sends to the server looks like this, see the work_experience, it's an array of dictionaries:
Don't mind the data values, it's taken on different times, mind the data structure in work_experience
It becomes like this when it reaches the server:
This is how the work_experience gets saved, which is wrong:
This is my post request:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:SAVE_USER_INFO_URL parameters:_userInformation success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
php function that receives the post request:
$params = $this->params()->fromPost();
$userId = $this->getUsersTable()->saveUserInfo($params);
$this->getSkillsTable()->saveSkills($params['skillset'], $userId);
$this->getWorkExperienceTable()->saveWorkExperience($params['work_experience'], $userId);
$view = new JsonModel($params);
return $view;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:SAVE_USER_INFO_URL parameters:_userInformation success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
You are limiting the app to only accept responses with header text/html, which is not correct because you are looking for json response. And you need to set the requestSerializer to a AFJSONRequestSerializer instance because the default is AFHTTPRequestSerializer

iOS : .Net API (asmx) Post Request With AFNetworking + AFHTTPRequestOperationManager

Hie all, I'm trying to send a post request on .net (asmx) API using AFNetworking class AFHTTPRequestOperationManager, here's my code that doing request,
-(void) request {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = #{#"test1": #"123",
#"test2": #"345"};
// manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/xml"];
[manager POST:#"http://somewebsite.com/getdetail.asmx/AllDetails" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
NSLog(#"%#",operation.description);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
NSLog(#"%#",operation.responseString);
}];
}
I'm getting the following response string
{"result":[],"status":"SUCCESS"}
And it prints the following error,
Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x996a630 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
What's going wrong?
How do I send post request using AFNetworking, to .net asmx API, is that a correct way?

Resources