Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting content-type overwriting httpbody?

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?

like image 980
Cheryl Yaeger Avatar asked Apr 21 '26 12:04

Cheryl Yaeger


1 Answers

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?

like image 141
user1509593 Avatar answered Apr 23 '26 02:04

user1509593



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!