I have now got details of the logs and the cause of the failure. To recap, the issue is that exactly the same POST request to a Web API works on Android and on the iOS simulator, but fails on actual iOS devices.
The error generated by the server is that the content-type header is not set. This is despite the fact that my code certainly does set the content-type header. To be precise, the server says the "Content-Type" header is not set, whereas my original code used "content-type" in lower case (as stated by the API), but I have changed this now and tried multiple variations, as well as setting the content-type to both "application/json" and "application/json; charset = utf-8"...but none of this makes any difference.
So the question is, why does setHeader("content-type", "application/json") work on Android and the iOS simulator, but not on an actual iOS device, and what can I do about this?
Details are as follows. My code looks like this:
...postResponse() and readResponse(InputStream input) methods omitted....
protected void buildRequestBody(OutputStream os) throws IOException {
os.write(json.getBytes("UTF-8")); //json contains the JSON string
}
};
r.setUrl(urlString); //the web API address
r.setPost(true);
r.addRequestHeader("Application-Key", "...");
r.addRequestHeader("Authorization", "...");
r.addRequestHeader("content-type", "application/json"); //NB...the content-type *is* set, but this somehow does not get loaded
InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
r.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueue(r);
The relevant parts of the server logs are as follows:
From a successful attempt from the iPhone simulator...
2013-10-28T13:45:54 PID[20272] Information Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
2013-10-28T13:45:54 PID[20272] Information Message='Action returned 'StatusCode: 201, ReasonPhrase: 'Created', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[XXX.xxxxx.WebApi.Models.BaseCardModel, XXX.xxxxx WebApi, Version=0.7.0.0, Culture=neutral, PublicKeyToken=null]], Headers:
{
Content-Type: application/json; charset=utf-8
}'', Operation=ReflectedHttpActionDescriptor.ExecuteAsync
From a failed attempt from the iPhone...
2013-10-28T13:44:57 PID[20272] Information Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
2013-10-28T13:44:57 PID[20272] Warning Message='UserMessage='The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.'', Operation=FormatterParameterBinding.ExecuteBindingAsync, Status=415 (UnsupportedMediaType), Exception=System.Web.Http.HttpResponseException: Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.