Reputation: 65
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" : @"[email protected]" , @"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":"[email protected]", "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.
Upvotes: 2
Views: 330
Reputation: 1866
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")
Upvotes: 0
Reputation: 38162
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";
Upvotes: 1