Problems with ASIFormDataRequest JSON Encoding

I seem to be facing a rather trivial issue that I cannot understand. I am using ASIFormDataRequest to interact with my Ruby on Rails application. I have a REST API that accepts a User object. I am using JSONKit to get a JSONString from an NSDictionary.

However, when I do [request setPostValue: [userObj JSONString] forKey: @ "user"];

The server-side request ends up leaving quotes. Basically,

{"password": "hello", "name": "user", "email": " user@foo.com "}

TO

"{\" password \ ": \" Hello \ "\" name \ ": \" user \ ", \" email \ ": \" user@foo.com \ "}"

It ends with tangled rails and he complains about an invalid object. Can I make ASIFormDataRequest not hide quotation marks? I understand that this may be a problem with JSONString itself, but I cannot find a good solution here.

thank

0
source share
2 answers

This is what worked for me:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
NSDictionary* inner = [[NSDictionary alloc] initWithObjectsAndKeys:username, @"login",password, @"password", nil];
NSDictionary* user = [[NSDictionary alloc] initWithObjectsAndKeys:inner, @"user", nil];
NSMutableData *requestBody = [[NSMutableData alloc] initWithData:[[user JSONString] dataUsingEncoding:NSUTF8StringEncoding]];

[request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request setRequestMethod:@"POST"];
[request setPostBody:requestBody];
[request startAsynchronous];
+2
source

You can’t mix ASIFormDataRequest and JSON - usually people encode things in HTML form data format or , they encode in JSON format. Not both!

JSON ? . ASIHTTPRequest ASIFormDataRequest :

NSString *postData = [userObj JSONString];
[request addRequestHeader: @"Content-Type" value: @"application/json; charset=utf-8"];
[request appendPostData:[postData dataUsingEncoding:NSUTF8StringEncoding]];

, , - - wireshark HTTP- ?

JSON Ruby?

0

All Articles