How to add lowercase field to NSURLRequest header field?

情到浓时终转凉″ 提交于 2019-12-06 07:48:52

问题


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?


回答1:


HTTP header fields are supposed to be case insensitive, so you need to fix your API.




回答2:


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.




回答3:


can u please try with addValue(_:forHTTPHeaderField:)?

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:MyURLString]];
[urlRequest addValue:@"aValue" forHTTPHeaderField:@"field"];


来源:https://stackoverflow.com/questions/2543396/how-to-add-lowercase-field-to-nsurlrequest-header-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!