问题
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?
回答1:
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?
来源:https://stackoverflow.com/questions/15254046/setting-content-type-overwriting-httpbody