Remove escape from serialized NSSdictionary - ios

I am pretty new to serializing with json, and I am facing a weird issue.
I am trying to send an NSURLRequest with a josn. The json is first stored into an NSSMutableDictionary and eventually is serialized. The serialized json object I get is escaped, meaning it has "\" in it all over the place.
The json is getting sent a server, but is getting denied. According to the admin the json is getting denied because its escaped. How can I removed all the back slashes from the serialized json before sending it.
HELP. I tried creating an NSString then converting to NSData then serialized and failed. I tried NSArray and failed. At least I think I did those correctly.
Did I make a mistake somewhere? is there a better way to do this?
Thanks,
Sam.

I came up with the same problem try AFNetworking
https://github.com/AFNetworking/AFNetworking
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
NSlog(#"value->%#", [responseObject objectForKey#"json_key"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

Related

AFNetworking View JSON Content For Error Code 3840

I am working on an app where I have AFNetworking fetching JSON requests from the my server. Now JSON is dynamically created based upon the POST variables I send from my app.
I was wondering when I get the following error message
JSON text did not start with array or object and option to allow fragments not set.
Is there a way to adapt my code so that I can actually see in the logger what the returned JSON request looks like. I have a feeling that there is a warning popping up in my PHP that is throwing off the JSON but I am not sure what it is.
Here is my code, it would make it so much easier if I could lets say NSLog the body content.
[manager POST:#"__MY__URL__" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
} progress:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
NSLog(#"Success: %#", responseObject);
if([[responseObject objectForKey:#"state"] isEqualToString:#"success"]){
//[self performSegueWithIdentifier:#"client_choose_ad" sender:self];
}else {
[self alertError:#"Unable To Create Advert" alertMessage:[responseObject objectForKey:#"message"]];
}
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Maybe printing the raw data of the request is what you are looking for.
How to print AFNetworking request as RAW data

Use AFNetworking to create a parse.com pointer object through REST API

I'm trying to use AFNetworking to call the parse.com REST API.
GET-Requests are working quite well so far. But when it comes to object creation I have a problem creating relations (pointers to object-ids) between two parse objects.
What I've tried so far:
NSString* parameterString = [NSString stringWithFormat:#"{\"__op\":\"AddRelation\",\"objects\":[{\"__type\":\"Pointer\",\"className\":\"MyClass\",\"objectId\":\"%#\"}]}", myClassObjectId];
NSDictionary *parameters = #{#"myClass": parameterString};
[self.manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { ... }];
Creating objects without pointers works well too. I think the error is related to my parameterString syntax:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)","code":111,"error":"invalid type for key myClass, expected *MyClass, but got string"
Any ideas? Thank you ;)
I got the answer, finally:
NSDictionary *params = #{#"myClass":#{#"__type":#"Pointer", #"className":#"MyClass", #"objectId":[foobar objectID]}};
[self.manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { ... }];
so... just plain JSON.

Yahoo OAuth Error 401 Consumer Key Unknown

I am trying to get a request token from Yahoo by making a POST request to the following URL:
https://api.login.yahoo.com/oauth/v2/get_request_token
I am making the request in the following way:
int randomNumber = rand();
NSDictionary *parameters = #{#"oauth_consumer_key": YahooConsumerKey,
#"oauth_signature_method": #"plaintext",
#"oauth_signature": YahooConsumerSecret,
#"oauth_version": #"1.0",
#"xoauth_lang_pref": #"en_us",
#"oauth_callback": #"oob",
#"oauth_timestamp": [self getTimeStamp],
#"oauth_nonce": [NSString stringWithFormat:#"%d", randomNumber]};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"https://api.login.yahoo.com/oauth/v2/get_request_token" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
The problem is that I keep getting the error 401 with the explanation that the consumer key is unknown. I have pasted the consumer key from where I've created my project, so I really don't know what I am doing wrong. Any help will be appreciated. Thanks!
Your request looks good. Try appending #"%26" to your YahooConsumerSecret string. Here's your request link with the fix, it seems to be working!
When using plaintext as oauth_signature_method you need to append %26 at the end of your oauth_signature according to the Yahoo docs.

AFNetworking 2.0 Request w. Custom Header

Ive tried to avoid asking such a newb question on here, but im a Android dev learning IOS and I cant figure out for the life of me how to add a simple header to my post requests using AFNetworking 2.0. Below is my code so far which works if i want to make a request that doesnt require a header.Could anyone show me via adding to my snippet or providing a alternate one that does this? I came across this tut: http://www.raywenderlich.com/30445 that shows how to add a header under the "A RESTful Class" heading , but its for afnetworking 1.0 and is now depreciated as far as i can tell.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"uid": #"1"};
AFHT
[manager POST:#"http://myface.com/api/profile/format/json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(#"JSON: %#", responseObject);
self.feedArray = [responseObject objectForKey:#"feed"];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
} ];
}
Under AFHTTPRequestOperationManager you will notice a property called requestSerializer. This is of type AFHTTPRequestSerializer, and requests made through the HTTP manager are constructed with the headers specified by this object.
So, try this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:#"SomeValue" forHTTPHeaderField:#"SomeHeaderField"]
//Make your requests
You can read the headers dictionary from the request serializer as follows:
manager.requestSerializer.HTTPRequestHeaders
Note that once you set a header this way, it will be used for all other operations!
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:#"staging" password:#"dustylendpa"];

AFHTTPRequestOperationManager urlstring encoding & Invalid parameter not satisfying: URLString

I'm loading json with afnetworking 2.0:
NSString *weatherUrl = #"http://www.souche.com/pages/xx/xx.json?request_message={\"type\":\"car-subdivision\"}";
weatherUrl = [weatherUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:weatherUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
console error:
'Invalid parameter not satisfying: URLString'
Is there need url encoding?
but i do it like this:
NSString *weatherUrl = #"http://www.souche.com/pages/dicAction/loadRootLevel.json?request_message={%22type%22:%22car-subdivision%22}";
still error!
How can i do?
It looks like there are a number of issues with the URL you're constructing, and the way you're passing (or not passing) parameters into AFNetworking. You don't need to construct your query string yourself, as AFNetworking will do that for you. As mentioned in my comment above, passing query=where UserName='abc' as part of a URL seems like a bad idea. However, here's a quick example of how you'd call AFNetworking's GET method if your URL was slightly different:
URL format:
https:////?username=abc&companyId=&page=1&pageSize=25&filterResultByColumns=true
you try this code :D

Resources