Rest WCF Post Json body parameter is always null while using Fiddler

fiddlerpostrestwcfweb services

I have been struggling with this the past few days. I have researched the issue and tried the solutions posted. However it has not worked. I have REST WCF Post method that has

     [OperationContract(Name = "ImportRawJson")]
    WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json,
            UriTemplate = "ImportRawJson/username/{username}/password/{password}/fileName/{fileName}")]
    string ImportRawJson(string username, string password, string fileName, string jsonStream);

I am able to consume this through web client. However when I try calling through Fiddler like below the body parameter always results in null and I get an exception.

Fiddler :
Post http://localhost/TimesheetService/Timesheet.svc/ImportRawJson/username/user/password/pwd/fileName/testfiddler

Request Headers:
User-Agent: Fiddler
Host: localhost
Content-Length: 32
Content-Type: application/json; charset=utf-8

Request Body:
{ "jsonStream":{ "ImportRaw": {"TestXml": {"xml": "test" } }}}

Error:
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 127
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Set-Cookie: ASP.NET_SessionId=wh4qxcu1x0vmiv45mmzuuaup; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Tue, 07 May 2013 14:00:58 GMT

{"ErrorCode":"Error","Message":"Procedure or function expects parameter 'jsonStream', which was not supplied."}

Any help as to how I can pass the body parameter. I truly appreciate. I am stuck at this point. Please help!! Thanks in advance

Best Answer

There are a couple of issues in your code. First, if by "JSON stream" you mean any JSON document, you won't be able to use the type string for your code. Instead, you'll need to take it as a Stream (which can basically accept any arbitrary input). If you take the input as a string, you should pass a JSON string to it. And since you set the body type to WrappedRequest, you need to wrap the JSON string in an object, with the parameter name being the member name, and the value you want to pass to your function the value. For example, to pass the string hello world to your operation, you'd need to pass this request body:

{"jsonStream":"hello world"}

But if I guessed correctly, and you want to take any arbitrary JSON, you need to go with the Stream parameter. The blog post at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx has more information about how to implement it.

Related Topic