3

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

Lurk
  • 31
  • 1
  • 2
  • What is the server seeing? Maybe the server is not responding properly. – Flyingdiver Aug 31 '11 at 14:24
  • Do you know if there's a way to see exactly what the request looks like before it processes it? The reason I ask is because I cannot access the logs on the server. – Lurk Aug 31 '11 at 14:31
  • Worth to mention is that curl works with the exact same header. Not sure if curl adds some default headers though. – Lurk Aug 31 '11 at 14:32
  • Did you solve this? I am now having the same issue. – Fogmeister Feb 26 '16 at 09:37

1 Answers1

0

May be use should provide Content-Type?

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];

Because "application/xml" is always passed in "Content-Type" header field.

Nekto
  • 17,837
  • 1
  • 55
  • 65
  • Unfortunately that's not the problem. I've added the Content-Type header and I still get the same result (JSON). – Lurk Aug 31 '11 at 14:30
  • I think you set incorrect header field. Now you should contact admin and ask exact name of header field. May be it is @"accept". Your request is created correctly. – Nekto Aug 31 '11 at 14:36
  • The http header field is case insensitive and it works with curl. Which means that the server should be working correctly. Do you know if curl adds any default headers that I'm missing in my request? – Lurk Aug 31 '11 at 14:38
  • Don't know any of default headers. Post code where you send request to server. – Nekto Aug 31 '11 at 14:43
  • There's not much more than setting up the request and initialize a connection: NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; I create an NSString from the NSData response and print it, that's now I know I get the response in JSON and not XML. – Lurk Aug 31 '11 at 14:49
  • Don't think that anybody could help you. Just contact web-server admin and ask him about right header fields. Your code is right – Nekto Aug 31 '11 at 14:57
  • Accept header: Client says I can accept these types of responses and I would like them with the priority stated by the header I've sent if available. Content-Type header: Server says you have received data. This is the type. – Carl Dec 28 '16 at 10:08