-->

How to add lowercase field to NSURLRequest header

2020-07-23 05:31发布

问题:

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"];