2

I'm making a POST request to a server from an IOS app and sending a JSON payload of an email and password value.

My code so far is making a NSDictionary and serializing it into NSData:

NSDictionary *dictionary = @{ @"email" : @"khush@gmail.com" , @"password" : @"mypass" };

NSData *payload = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];

I attach the payload as part of the request.HTTPBody, however the server receives the following data:

{ '{"email":"khush@gmail.com", "password":"mypass"}' : ' ' }

It seems to be taking the whole string as the key and emitting a null as the value.

Any ideas/solutions to this problem? I've had a look at but it doesn't make See this link sense to me.

Community
  • 1
  • 1
KhushV
  • 55
  • 1
  • 3
  • 4
    It's not the conversion from dictionary -> JSON, it's the formatting within the HTTP request that is failing. – trojanfoe Sep 14 '15 at 10:00
  • 2
    +1 @trojanfoe How do you "attach" the payload as a part of the HTTP body? And does your server support JSON as a part of the request's body? – Amin Negm-Awad Sep 14 '15 at 10:02
  • Easiest way is to debug the JSON string before you attach it. If its valid then something down the line is to blame. – Aris Sep 14 '15 at 10:16
  • Try converting NSData to NSString and pass this string to server as some web developers accept json string and not object. Example: NSString *strJson = [[NSString alloc]initWithData:payload encoding:NSUTF8StringEncoding]; request.httpBody = strJson; – Milan Agarwal Sep 14 '15 at 12:14
  • Your question is wrong. You are asking "why" and the answer is "it isn't, your problem is somewhere else". – gnasher729 Sep 14 '15 at 13:16
  • im adding json to the body this way: request.HTTPBody = payload; – KhushV Sep 14 '15 at 13:39
  • @KhushV I added an update part to my answer, HTTPBody is not designed to contain only a value, but a NSData representing a string of the kind key1=value1&key2=value2 (like GET parameters), what you are receiving: a key without an object is NORMAL given you didn't respect the key=value[&...] format. – Jerome Diaz Sep 14 '15 at 14:15

2 Answers2

1

Nothing is wrong in your payload. Data is being messed up in HTTP layer. Please ensure you have following content type set in your request headers.

"Content-Type" = "application/json";
Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • Well the server *is* receiving JSON, it's just messed-up, so I doubt that is the issue. – trojanfoe Sep 14 '15 at 10:12
  • You are right. But if content type is mistakenly set as `application/x-www-form-urlencoded` then it reaches as JSON but in messed up form. Lets see if this helps OP. – Abhinav Sep 14 '15 at 10:16
  • Holy cow! Almost a day lost with this and I had no clue. To do it in Swift 3: `request.setValue("application/json", forHTTPHeaderField: "Content-Type")` – Rodrigo Pinto Aug 08 '17 at 01:17
0

Seems like you are making something like

request.HTTPBody = encodedVersionOfPayload;

while you should be marking something more like

request.HTTPBody = [NSString stringWithFormat:@"%@=%@", yourKey, encodedVersionOfPayload];

Please show us this part of your code.


UPDATE

After a quick look to documentation, NSMutableURLRequest, HTTPBody property is a NSData and not a NSString,

so you have probably written

request.HTTPBody = payload;

where you should have something more like

NSString *jsonString = [[NSString alloc] initWithData:payload encoding:NSUTF8StringEncoding];
NSString *payloadString = [NSString stringWithFormat:@"data=%@", jsonString];
request.HTTPBody = [payloadString dataUsingEncoding:NSUTF8StringEncoding];

(not tested but should be correct, your json object should be available on your server using the key "data")

Jerome Diaz
  • 1,746
  • 8
  • 15