没有使用内容类型的应用程序/ JSON iOS获得JSON响应 [英] Not getting JSON response using content-type application/JSON iOS

查看:134
本文介绍了没有使用内容类型的应用程序/ JSON iOS获得JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户已经获得了 REST 服务,我应该为我的iOS获得 JSON 响应我使用 content-type 作为application / json,但默认情况下它被视为text / html。我在我的Mozilla浏览器中的 REST 客户端中使用application / json对内容类型进行交叉检查标题部分,我可以看到正确的响应。使用下面的代码,我从服务器收到 400 错误。我总是可以看到text / html默认用作 400 响应的一部分

I have been provided with a REST service from my customer for which I should get JSON response for my iOS code.I am using content-type as "application/json" but somehow it is taken as "text/html" by default. I cross checked this in a REST client in my Mozilla browser using "application/json" for the content-type in header section and I could see the proper response.On using the below code, I am getting a 400 error from the server. I could always see "text/html" as being used as part of the 400 response by default

NSString * strBodyOnlineStart = [NSString stringWithFormat:@"&email=***@***.com&deviceCode=*****&password_value=***"];

      NSURL *urlOnlineStart = [NSURL
                          URLWithString:@"http://local.server/xxxxx"];
        NSMutableURLRequest *requestOnlineStart = [NSMutableURLRequest
                                                   requestWithURL:urlOnlineStart];


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

        [requestOnlineStart setHTTPMethod:@"POST"];
        NSData *requestBodyOnlineStart = [strBodyOnlineStart
                                          dataUsingEncoding:NSUTF8StringEncoding];
        [requestOnlineStart setHTTPBody:requestBodyOnlineStart];

推荐答案

我已经构建了许多服务器REST服务和许多服务移动客户端应用程序访问它们并遇到无数次这样的问题。所以我知道你的痛苦,我用自己的一套工具解决了这些问题(我喜欢Windows上的Fiddler和Mac上的Charles)并且学到了许多失败原因。

I have built many server REST services and many mobile client apps to access them and run into issues like this countless times. So I know your pain, and I've solved these problems with my own set of tools (I like Fiddler on Windows and Charles on Mac) and learned many reasons for failures.

第一次观察:

没有工作请求的全部细节&响应对,以及失败的请求&响应对,找不到官方解决方案。但我很确定如果您提供这些,官方解决方案会很简单。

Without the full details of a working request & response pair, and a failing request & response pair, finding the official solution isn't possible. But I'm pretty sure if you provided those, the official solution would be simple.

测试

为了测试你的问题,我安装了 REST客户端在Firefox中,在我的开发机器上设置新服务。另外,我在OS X上使用了Charles Proxy和编辑请求函数来自定义原始请求。

To test your problem, I installed REST client in Firefox and setup a new service on my development machine. Also, I used Charles Proxy on OS X with the edit request function to customize a raw request.


我在REST客户端交叉检查了这个在我的Mozilla浏览器中使用
application / json作为标题部分中的内容类型,我可以
看到正确的响应。

I cross checked this in a REST client in my Mozilla browser using "application/json" for the content-type in header section and I could see the proper response.

奇怪你说它有效。我试过了,服务器无法解析正文值。我正在使用的服务器端平台不是ASP.NET,但我只是在进行任何平台的标准预期行为。可能您的REST客户端实际上没有提交标头。我想知道你是否将标题切换到某个任意值,如 application / notjson ,看看它是否仍然有效(然后我假设它是REST客户端覆盖)。

Odd that you say it worked. I tried that and the server failed to parse the body values. The server-side platform I'm using isn't ASP.NET, yet I'm just going on the standard expected behaviour of any platform. Possibly your REST client was not actually submitting the header. I wonder if you switched the header to some arbitrary value like application/notjson and see if it works still (then I'd assume it was the REST client overriding).

可能的原因

如上面的报价所示,你会给人一种想要的印象在响应 Content-Type 标题中接收 application / json ,似乎您认为必须发送<$请求中的c $ c> Content-Type 标头以匹配该响应。 不应该是必要的。服务器应该能够以标准格式接收您的请求,如 application / x-www-form-urlencoded ,这是典型的服务器,以JSON响应正如您所期待的那样

As given in the quote above, you give the impression of wanting to receive application/json in the response Content-Type header, and it seems you think you must send the Content-Type header in the request to match that response. That should not be necessary. The server should be able to receive your request in a standard format as application/x-www-form-urlencoded, that is typical of servers, and respond with JSON as you're expecting.

通常,网络服务器自然地接受json作为请求正文格式,同样,他们通常接收传入请求Content-Type of application / json。 但是,我见过他们这样做的案例。

Usually, web servers do not naturally accept json as a request body format, and similarly, they usually do not receive incoming request Content-Type of application/json. But, I've seen cases where they do.

无论如何,提交冲突内容类型和正文格式的请求绝对是一个失败的设置。

In any case, submitting a request with conflicting Content-Type and body formats is definitely a failing setup.

我发现我的服务器测试用JSON回复,使用你的& email = *** @ ***。com& deviceCode = *****& password_value = *** 正文,将标题切换为 application / x-www-form-urlencoded ,application / json。典型的善良。

I found my server test responding with JSON, using your &email=***@***.com&deviceCode=*****&password_value=*** body, after switching the header to application/x-www-form-urlencoded, application/json. Typical goodness.

代码

尝试注释掉以下行:

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

或将其更改为:

    [requestOnlineStart setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

这篇关于没有使用内容类型的应用程序/ JSON iOS获得JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆