[英]Not getting JSON response using content-type application/JSON iOS
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. 我已经从我的客户那里获得了一个
REST
服务,我应该为我的iOS代码获得JSON
响应。我使用content-type
作为“application / json”但是默认情况下它被视为“text / html”。 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. 我在Mozilla浏览器的
REST
客户端中使用“application / json”对头部分中的content-type
进行交叉检查,我可以看到正确的响应。使用下面的代码,我从服务器收到400
错误。 I could always see "text/html" as being used as part of the 400
response by default 我总是可以看到“text / html”默认用作
400
响应的一部分
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];
I have built many server REST services and many mobile client apps to access them and run into issues like this countless times. 我已经构建了许多服务器REST服务和许多移动客户端应用程序来访问它们并遇到无数次这样的问题。 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.
所以我知道你的痛苦,我用自己的一套工具解决了这些问题(我喜欢Windows上的Fiddler和Mac上的Charles)并且学到了许多失败原因。
First observation: 首先观察:
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.
但我很确定如果你提供这些,官方的解决方案会很简单。
Testing 测试
To test your problem, I installed REST client in Firefox and setup a new service on my development machine. 为了测试你的问题,我在Firefox中安装了REST客户端 ,并在我的开发机器上设置了一个新服务。 Also, I used Charles Proxy on OS X with the edit request function to customize a raw request.
此外,我在OS X上使用了Charles Proxy和编辑请求功能来自定义原始请求。
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.
我在Mozilla浏览器的REST客户端中使用“application / json”对头部分中的内容类型进行交叉检查,我可以看到正确的响应。
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.
我正在使用的服务器端平台不是ASP.NET,但我只是在进行任何平台的标准预期行为。 Possibly your REST client was not actually submitting the header.
可能您的REST客户端实际上并未提交标头。 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). 我想知道你是否将标题切换到某个任意值,如
application/notjson
,看看它是否仍然application/notjson
(然后我假设它是REST客户端覆盖)。
Probable cause 可能的原因
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. 如上面引用的那样,您给出了想要在响应
Content-Type
标头中接收application/json
的印象,并且您认为必须在请求中发送Content-Type
标头以匹配该响应。 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 . 服务器应该能够以标准格式接收您的请求,如
application/x-www-form-urlencoded
,这是典型的服务器,并按照您的期望使用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. 通常,Web服务器自然不接受json作为请求体格式,同样,它们通常不接收传入请求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. 无论如何,提交具有冲突的 Content-Type和body格式的请求绝对是一个失败的设置。
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. 在将标题切换到
application/x-www-form-urlencoded
,我发现我的服务器测试使用您的& &email=***@***.com&deviceCode=*****&password_value=***
与&email=***@***.com&deviceCode=*****&password_value=***
进行了回复,并使用您的& &email=***@***.com&deviceCode=*****&password_value=***
.*。应用程序/ JSON。 Typical goodness. 典型的善良。
Code 码
Try commenting-out the following line: 尝试评论出以下行:
[requestOnlineStart setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
or change it to: 或将其更改为:
[requestOnlineStart setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
Let's try (see my comment): 我们试试(看我的评论):
// replace strBodyOnlineStart NSDictionary
// NSString * strBodyOnlineStart = [NSString stringWithFormat:@"&email=***@***.com&deviceCode=*****&password_value=***"];
NSDictionary *requestObj = @{
@"email": your email,
@"passwordValue": your password,
};
// Can you try jsonRequest with NSJSONSerialization framework (iOS framework default).
NSData *jsonRequest = [NSJSONSerialization dataWithJSONObject:strBodyOnlineStart options:0 error:&error];
// your url has "/" value at the end or not???
NSString *url = @"http://local.server/xxxxx/";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:jsonRequest];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//try set value like this
[request setValue:[NSString stringWithFormat:@"%d", [jsonRequest length]] forHTTPHeaderField:@"Content-Length"];
// Hope this format helps you.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.