Submitting an HTTP POST request with Content-type of application/json will still send as application/x-www-form-urlencoded

Originator:damiancarrillo
Number:rdar://20489846 Date Originated:2015-04-09
Status:Closed Resolved:Duplicate of 20340623
Product:iOS SDK Product Version:8.3 (12F70)
Classification: Reproducible:Always
 
Summary:
Configure an NSURLSession with an NSURLSessionConfiguration that has an explicit HTTPAdditionalHeaders property that specifies a Content-type of application/json. Use that session to produce a data task, then subsequently resume that task so the HTTP request is made. The request will be made with a Content-type of application/x-www-form-urlencoded, and the HTTPBody will also be form-encoded.

Steps to Reproduce:
The following steps are relative to the ViewController.m file in the sample project.

1. Configure a default NSURLSessionConfiguration (line 20)
2. Specify HTTPAdditionalHeaders with a Content-type of application/json (line 21)
3. Create an NSMutableURLRequest (line 34)
4. Specify POST as the HTTPMethod (line 35)
5. Specify an HTTPBody  that contains JSON data (line 36)
6. Create a data task with the request (line 38)
7. Resume the task (line 46)

Expected Results:
I expected the request to be POSTed with a Content-type header of application/json and the HTTPBody to not be form encoded.

Actual Results:
The request was POSTed as application/x-www-form-urlencoded and the HTTPBody was form encoded. I know this because I ran the device through mitmproxy. It produced the following request:

2015-04-09 16:36:17 POST http://www.example.com/

Host:              www.example.com
Content-Type:      application/x-www-form-urlencoded
Connection:        keep-alive
Proxy-Connection:  keep-alive
Accept:            */*
User-Agent:        HTTPContentType/1 CFNetwork/711.3.18 Darwin/14.0.0
Accept-Language:   en-us
Content-Length:    120
Accept-Encoding:   gzip, deflate
URLEncoded form
{                               
  "quote" : "Once an SDK has    
been expanded by a code         
modification, it will never     
return to its original          
dimensions."                    
}:           





Version:
iOS 8.3 (12F70)

Notes:
The iOS 8.3 documentation for NSURLSessionConfiguration.HTTPAdditionalHeaders does not indicate that the Content-type header should be avoided. This worked in previous versions of iOS.

Configuration:
iPhone 6+ 64 GB

Attachments:
'HTTPContentType.zip' was successfully uploaded.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!