3

I'm having a weird issue calling an API from my ios app. I create a post request

NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:url
                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                               timeoutInterval:5.0];
[request setHTTPMethod:@"POST"];

I then create json to send to the request and add it to the body of the request like so:

[request setHTTPBody:encodedData];

Finally, I am adding a few cookies which I then set with:

NSArray* cookieArray = [NSArray arrayWithObjects: sessionCookie, uidCookie, vidCookie, nil];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];
[request setAllHTTPHeaderFields:headers];

This all works fine. However, since I am sending json I'm trying to be good and also set the content type with this line of code:

[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField: @"Content-Type"];

As soon as I add this line of code, the data I sent to the body of the request no longer gets passed as part of the request. Does this sound like behavior anyone else has experienced?

Cheryl Yaeger
  • 69
  • 1
  • 6

1 Answers1

3

I am also using

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

Check this explanation What does "Content-type: application/json; charset=utf-8" really mean?

Community
  • 1
  • 1
user1509593
  • 1,025
  • 1
  • 10
  • 20