在 iOS 上使用带有 NSMutableURLRequest 的接受标头
我的 NSMutableURLRequest 有问题。我的服务器支持 JSON 和 XML 格式,并且它们通过访问标头分隔。如果未设置访问标头,它也默认为 JSON。 这基本上意味着,当我想要 XML 格式的响应时,我需要创建一个使用“application/xml”作为访问标头的请求。
我现在面临的问题是,即使我传入正确的访问标头来获取 XML 中的响应,我仍然会得到 JSON(因为这是默认值)。这就像我的请求忽略了访问标头。为了使我的请求能够使用标头,我还需要创建其他内容吗?
请求非常简单:NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
我已通过打印 allHTTPHeaderFields 确认我的请求包含我的标头:标题:{
接受=“application/xml”;
}
I have a problem with my NSMutableURLRequest. My server supports both JSON and XML formats and they are separated with an access header. It also defaults to JSON if no access header is set.
Which basically means that when I want the response in XML I need to create a request with 'application/xml' as access header.
The problem I'm facing now is that even if I pass in the correct access header to get the response in XML I still end up with JSON (because that's default). It's like my request disregards the access header. Is there anything else I need to create in order to make my request work with headers?
The request is really simple:NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
I have confirmed that my request contains my headers by printing allHTTPHeaderFields:headers: {
Accept = "application/xml";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能应该提供
Content-Type
?因为“application/xml”始终在“Content-Type”标头字段中传递。
May be use should provide
Content-Type
?Because "application/xml" is always passed in "Content-Type" header field.