restsharp/RestSharp — Issue #221: Content-Type Header not respected on post
Author: itanex
Created: 2012-02-14 01:32:59+00:00
State: open

Line of issue:
https://github.com/restsharp/RestSharp/blob/master/RestSharp/Http.cs#L277

* webRequest.ContentType = "application/x-www-form-urlencoded";

When attempting a post to WCF service endpoint, the content-type value provided is not being respected. There are two possible solutions that could be applied.

* A content-type header if not provided is not attempted to be placed on the request under any method type
* If a post method type is provided and no content-type is provided, then the content type of "application/x-www-form-urlencoded" should be placed on the header

I believe #1 to be the best solution since there is no specification that an HTTP POST MUST include a Content-Type header This line causes any specific post content-type header applied to the request to be over-ridden. In the process of a WCF endpoint evaluation, the request is being refused because the content-type is not representative of the body content.

Message received from WCF service:

* The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.

A request with a restSharp client fails and looks like this.

-----------------------------
REQUEST:
POST http://localhost.partnerapi.testsite.com/lightbox/?access_token=bee9fd3f-7a16-4dc0-8102-ca5c2e566060 HTTP/1.1
Accept: application/JSON
Content-Type: application/x-www-form-urlencoded
Cotent-Length: 27
Host: localhost.partnerapi.testsite.com

{
"LightboxName":"cats"
}

RESPONSE:
HTTP/1.1 500 INTERNAL SERVER ERROR
Cache-Control: private
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 14 Feb 2012 00:42:09 GMT
Content-Length: ...[TRUNCATED for illustration]

-----------------------------

A successful request to the endpoint looks like this.

-----------------------------
REQUEST:
POST http://localhost.partnerapi.testsite.com/lightbox/?access_token=bee9fd3f-7a16-4dc0-8102-ca5c2e566060 HTTP/1.1
Accept: application/JSON
Content-Type: application/JSON
Cotent-Length: 27
Host: localhost.partnerapi.testsite.com

{
"LightboxName":"cats"
}

RESPONSE:
HTTP/1.1 200 OK
Cache-Control: private
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 14 Feb 2012 00:42:09 GMT
Content-Length: 0

-----------------------------