【问题标题】:setting content-type overwriting httpbody?设置内容类型覆盖httpbody?
【发布时间】:2013-02-21 15:20:42
【问题描述】:

我在从我的 ios 应用调用 API 时遇到了一个奇怪的问题。我创建了一个帖子请求

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

然后我创建 json 以发送到请求并将其添加到请求的正文中,如下所示:

[request setHTTPBody:encodedData];

最后,我添加了一些 cookie,然后设置:

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

这一切都很好。但是,由于我正在发送 json,因此我正在努力做好并使用这行代码设置内容类型:

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

添加这行代码后,我发送到请求正文的数据不再作为请求的一部分传递。这听起来像其他人经历过的行为吗?

【问题讨论】:

  • 如果在设置body之前设置header怎么办?
  • 试过了,没有骰子。还是谢谢。
  • 您是否验证过数据包嗅探器发生了什么?
  • 你解决了吗?

标签: ios objective-c nsmutableurlrequest


【解决方案1】:

我也在用

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

检查这个解释 What does "Content-type: application/json; charset=utf-8" really mean?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2021-09-22
    相关资源
    最近更新 更多