Before today's iOS 8.3 update I had my code working right, but after updating server started rejecting requests as it could not find JSON data.
在今天的iOS 8.3更新之前,我的代码工作正常,但在更新服务器后开始拒绝请求,因为它无法找到JSON数据。
I found that iOS is sending a wrong application/x-www-form-urlencoded text (not properly encoded as it seems a JSON object):
我发现iOS发送了一个错误的application / x-www-form-urlencoded文本(没有正确编码,因为它似乎是一个JSON对象):
This is what I expected to be sent (and what was sent on 8.2):
这是我期望发送的内容(以及8.2发送的内容):
As I said, this only happens on iOS 8.3 (I just tried on iOS simulator with 8.2 and it works).
正如我所说,这只发生在iOS 8.3上(我刚刚尝试使用8.2的iOS模拟器,它的工作原理)。
I think the problem is in one of the classes that appear on these lines:
我认为问题出现在这些行中的一个类中:
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:requestDict options:0 error:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPBody = bodyData;
I checked Apple documentation and none appears as modified recently.
我检查了Apple文档,最近没有出现任何修改。
Is someone suffering the same or knows what can lead to this?
有人遭受同样的痛苦或知道会导致什么?
Thanks.
谢谢。
3 个解决方案
#1
7
I put the comment as answer here:
我把评论作为答案在这里:
Try to indicate the http method like
尝试指出http方法
[request setHTTPMethod:@"POST"]
and / or, the request length
和/或请求长度
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
and / or the content type
和/或内容类型
[request setValue:@"application/<YOUR CONTENT TYPE>"forHTTPHeaderField:@"Content-Type"];
#2
11
I also have encountered this problem in one application.
我也在一个应用程序中遇到过这个问题。
It looks like it could be a bug introduced in iOS 8.3. From the official Framework Reference, it is said of the NSURLSessionConfiguration
's HTTPAdditionalHeaders
property:
看起来它可能是iOS 8.3中引入的错误。从官方框架参考中,可以看出NSURLSessionConfiguration的HTTPAdditionalHeaders属性:
This property specifies additional headers that are added to all tasks within sessions based on this configuration. For example, you might set the User-Agent header so that it is automatically included in every request your app makes through sessions based on this configuration.
此属性指定基于此配置添加到会话中的所有任务的其他标头。例如,您可以设置User-Agent标头,以便它自动包含在您的应用根据此配置通过会话进行的每个请求中。
From what I understand, a Content-Type set in this configuration should be automatically included in every request made through this session, which is no longer the case since 8.3. I've filed a radar.
据我所知,此配置中的Content-Type集应自动包含在通过此会话发出的每个请求中,自8.3以来不再是这种情况。我已经提交了雷达。
UPDATE: The radar has been set as 'duplicate' of another so it's more likely a bug.
更新:雷达被设置为另一个的“重复”,所以它更可能是一个错误。
As Luca pointed out, the work around is to set the Content-Type directly on the request:
正如Luca所指出的,解决方法是直接在请求上设置Content-Type:
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
资料来源:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSessionConfiguration_class/#//apple_ref/occ/instp/NSURLSessionConfiguration/HTTPAdditionalHeaders
#3
0
It seems that iOS 8.3 introduces this. At my site we got found the problem that the (json)body was correctly not set inside the request.
似乎iOS 8.3引入了这个。在我的网站上,我们发现了(json)正文未在请求中设置的问题。
When debugging try to debug the server-side also
调试时尝试调试服务器端
#1
7
I put the comment as answer here:
我把评论作为答案在这里:
Try to indicate the http method like
尝试指出http方法
[request setHTTPMethod:@"POST"]
and / or, the request length
和/或请求长度
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
and / or the content type
和/或内容类型
[request setValue:@"application/<YOUR CONTENT TYPE>"forHTTPHeaderField:@"Content-Type"];
#2
11
I also have encountered this problem in one application.
我也在一个应用程序中遇到过这个问题。
It looks like it could be a bug introduced in iOS 8.3. From the official Framework Reference, it is said of the NSURLSessionConfiguration
's HTTPAdditionalHeaders
property:
看起来它可能是iOS 8.3中引入的错误。从官方框架参考中,可以看出NSURLSessionConfiguration的HTTPAdditionalHeaders属性:
This property specifies additional headers that are added to all tasks within sessions based on this configuration. For example, you might set the User-Agent header so that it is automatically included in every request your app makes through sessions based on this configuration.
此属性指定基于此配置添加到会话中的所有任务的其他标头。例如,您可以设置User-Agent标头,以便它自动包含在您的应用根据此配置通过会话进行的每个请求中。
From what I understand, a Content-Type set in this configuration should be automatically included in every request made through this session, which is no longer the case since 8.3. I've filed a radar.
据我所知,此配置中的Content-Type集应自动包含在通过此会话发出的每个请求中,自8.3以来不再是这种情况。我已经提交了雷达。
UPDATE: The radar has been set as 'duplicate' of another so it's more likely a bug.
更新:雷达被设置为另一个的“重复”,所以它更可能是一个错误。
As Luca pointed out, the work around is to set the Content-Type directly on the request:
正如Luca所指出的,解决方法是直接在请求上设置Content-Type:
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
资料来源:https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSessionConfiguration_class/#//apple_ref/occ/instp/NSURLSessionConfiguration/HTTPAdditionalHeaders
#3
0
It seems that iOS 8.3 introduces this. At my site we got found the problem that the (json)body was correctly not set inside the request.
似乎iOS 8.3引入了这个。在我的网站上,我们发现了(json)正文未在请求中设置的问题。
When debugging try to debug the server-side also
调试时尝试调试服务器端