setting content-type overwriting httpbody?

╄→尐↘猪︶ㄣ 提交于 2019-12-14 01:26:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!