如何将小写字段添加到 NSURLRequest 标头字段?
我对如何向 NSMutableURLRequest 添加小写标头字段感到非常沮丧。
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:MyURLString]];
[urlRequest setValue:@"aValue" forHTTPHeaderField:@"field"];
在上面的示例中,“field”切换为“Field”,因为标头字段名称不区分大小写。我认为这不应该发生,但它确实发生了。我正在使用的 API 区分大小写,因此我的 GET 请求被忽略。有什么办法可以覆盖 case 开关吗?
I'm getting pretty frustrated figuring out how to add a lowercase header field to an NSMutableURLRequest.
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:MyURLString]];
[urlRequest setValue:@"aValue" forHTTPHeaderField:@"field"];
In the example above, "field" gets switched to "Field," since the header field names are case insensitive. I would think this shouldn't happen, but it does. The API I am working with is case sensitive, so my GET request is ignored. Is there any way to override the case switch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以尝试使用 addValue(_:forHTTPHeaderField:) 吗?
can u please try with addValue(_:forHTTPHeaderField:)?
HTTP 标头字段应该不区分大小写,因此您需要修复你的API。
HTTP header fields are supposed to be case insensitive, so you need to fix your API.
我写了一篇关于该主题的博客文章: 修复 -[NSMutableURLRequest setValue:forHTTPHeaderField: ]我建议你阅读一下。您将了解如何解决此问题以及为什么不应该解决它。
I have written a blog post on the topic: Fixing -[NSMutableURLRequest setValue:forHTTPHeaderField:] I suggest you to read it. You will learn how to fix this problem and why you should not fix it.