Option to Disable/Enable Content-Type Validation

Feb 15, 2013 at 6:22 PM
Hi AustinHarris,
First at all thanks for this promissing implementation of Json-rpc.
I'm starting a new project (systems integration)but one of the bizarre requirements I have is to support request with a Content-type application/x-www-form-urlcoded.
How Could I accomplish this?

Thanks in advance
Coordinator
Feb 15, 2013 at 6:24 PM
This should already be supported.

Could you post an example of the request you are making that is failing?
Feb 15, 2013 at 6:46 PM
Edited Feb 15, 2013 at 6:49 PM
Sure, using powershell's iwr

when I try this:

iwr -Uri localhost:xxx/x.rpc -Method Post -ContentType application/x-www-form-urlencoded -body $jsonobject

return the exception Invalid Request The JSON sent is not a valid Request Object.

but when I change the Content type to application/json the same request returns OK

iwr -Uri localhost:xxx/x.rpc -Method Post -ContentType application/json -body $jsonobject


I just starting to see the source code and in AustinHarris.JsonRpc.AspNet.JsonRpcHandler.GetJsonRpcString method it seems to be the validation. Is this correct?

I'm also new to Asp.net so sorry if this is a stupid cuestion. :|
Coordinator
Feb 15, 2013 at 6:47 PM
Can you run fiddler can get a capture of the HTTP traffic. I would like to see the actual http request
Feb 15, 2013 at 8:06 PM
Hi again,
Using Fiddler both request returns Ok, maybe I'm doing something wrong with Invoke-WebRequest :( , so this mean there is no Content-type Validation at all?
Coordinator
Feb 15, 2013 at 8:34 PM
I bet I know the issue.

From your example your -body is the same in both examples.
For the -ContentType application/w-www-form-urlencoded call, you will need to associate your $jsonobject with a form variable like:
$formFields = @{jsonrpc=$jsonobject}
iwrt -Uri $myUrl -Method Post  -ContentType "application/x-www-form-urlencoded" -Body $formFields
There is a contentType check, see JsonRpchandler.cs JsonRpcHandler.GetJsonRpcString

If the Method = Post, and ContentType = "application/x-www-form-urlencoded" then the handler expects the JsonRPC to be the value of a form variable called 'jsonrpc'.
If the Method = Post, and ContentType is anything else, then it expects the HTTP body to be the JsonRPC.
If the Method = Get, then it expects a GET parameter named 'jsonrpc' with its value being the JsonRPC.

It looks to me as though you are passing the JsonRPC in the body without assigning it to a form variable named 'jsonrpc'.

Try making that change and let me know if it works.
Coordinator
Feb 15, 2013 at 11:11 PM
Ok, I got a little time to test it with powershell, and got it working. Here is are examples that works against the latest sources when you run the test server.
PS C:\Users\Austin> Invoke-WebRequest -Uri Http://localhost.:49718/json.rpc -Method Post -Body @{jsonrpc="{'method':'internal.echo','params':['Echo This'],'id':2}"}

StatusCode        : 200
StatusDescription : OK
Content           : {"jsonrpc":"2.0","result":"Echo This","id":2}
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Connection: Close
                    Cache-Control: private
                    Content-Type: text/html; charset=utf-8
                    Date: Fri, 15 Feb 2013 23:04:06 GMT
                    Server: ASP.NET Development Server/...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Connection, Close], [Cache-Control, private], [Content-Type,
                    text/html; charset=utf-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 45
Oh, and you can ignore the extra '.' after the localhost, that was just there to let fiddler pickup the traffic.
PS C:\Users\Austin> Invoke-WebRequest -Uri Http://localhost.:49718/json.rpc -Method Post -Body @{jsonrpc="{'method':'tes
tArbitraryJObject','params':[{'myparam':'myvalue','myOtherParam':7}],'id':2}"}


StatusCode        : 200
StatusDescription : OK
Content           : {"jsonrpc":"2.0","result":{"myparam":"myvalue","myOtherParam":7},"id":2}
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Connection: Close
                    Cache-Control: private
                    Content-Type: text/html; charset=utf-8
                    Date: Fri, 15 Feb 2013 23:08:43 GMT
                    Server: ASP.NET Development Server/...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Connection, Close], [Cache-Control, private], [Content-Type,
                    text/html; charset=utf-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 72
Notice I'm not explicitly setting the ContentType, but it seems that 'iwr' defaults to "application/x-www-form-urlencoded" anyhow.

Here are the requests as fiddler picked them up.
POST http://localhost.:49718/json.rpc HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.2; en-US) WindowsPowerShell/3.0
Content-Type: application/x-www-form-urlencoded
Host: localhost.:49718
Content-Length: 102

jsonrpc=%7b%27method%27%3a%27internal.echo%27%2c%27params%27%3a%5b%27Echo+This%27%5d%2c%27id%27%3a2%7d
POST http://localhost.:49718/json.rpc HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.2; en-US) WindowsPowerShell/3.0
Content-Type: application/x-www-form-urlencoded
Host: localhost.:49718
Content-Length: 154
Connection: Keep-Alive

jsonrpc=%7b%27method%27%3a%27testArbitraryJObject%27%2c%27params%27%3a%5b%7b%27myparam%27%3a%27myvalue%27%2c%27myOtherParam%27%3a7%7d%5d%2c%27id%27%3a2%7d
Hope that works for you.

-Austin
Coordinator
Feb 15, 2013 at 11:24 PM
Also, because your using PowerShell, I'm assuming you may be processing batches?

Just so you know, JsonRpc.Net supports batched RPC calls to improve performance.

Here is an example that batches 5 JsonRpc requests. You can correlate the responses to the requests by the ID field.
PS C:\Users\Austin> Invoke-WebRequest -Uri Http://localhost.:49718/json.rpc -Method Post -Body @{jsonrpc="[{'method':'te
stArbitraryJObject','params':[{'myparam':'myvalue','myOtherParam':7}],'id':2},{'method':'internal.echo','params':['Echo
This'],'id':2},{'method':'myIp','params':[],'id':3},{'method':'myUserAgent','params':[],'id':4},{'method':'testDateTime'
,'params':[],'id':5}]"}


StatusCode        : 200
StatusDescription : OK
Content           : [{"jsonrpc":"2.0","result":{"myparam":"myvalue","myOtherParam":7},"id":2},{"jsonrpc":"2.0","result"
                    :"Echo
                    This","id":2},{"jsonrpc":"2.0","result":"::1","id":3},{"jsonrpc":"2.0","result":"Mozilla/5.0 (...
RawContent        : HTTP/1.1 200 OK
                    Transfer-Encoding: chunked
                    Connection: Close
                    Cache-Control: private
                    Content-Type: text/html; charset=utf-8
                    Date: Fri, 15 Feb 2013 23:18:58 GMT
                    Server: ASP.NET Development Server/...
Forms             : {}
Headers           : {[Transfer-Encoding, chunked], [Connection, Close], [Cache-Control, private], [Content-Type,
                    text/html; charset=utf-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 336
And here is the request from Fiddler.
POST http://localhost.:49718/json.rpc HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.2; en-US) WindowsPowerShell/3.0
Content-Type: application/x-www-form-urlencoded
Host: localhost.:49718
Content-Length: 491
Connection: Keep-Alive

jsonrpc=%5b%7b%27method%27%3a%27testArbitraryJObject%27%2c%27params%27%3a%5b%7b%27myparam%27%3a%27myvalue%27%2c%27myOtherParam%27%3a7%7d%5d%2c%27id%27%3a2%7d%2c%7b%27method%27%3a%27internal.echo%27%2c%27params%27%3a%5b%27Echo+This%27%5d%2c%27id%27%3a2%7d%2c%7b%27method%27%3a%27myIp%27%2c%27params%27%3a%5b%5d%2c%27id%27%3a3%7d%2c%7b%27method%27%3a%27myUserAgent%27%2c%27params%27%3a%5b%5d%2c%27id%27%3a4%7d%2c%7b%27method%27%3a%27testDateTime%27%2c%27params%27%3a%5b%5d%2c%27id%27%3a5%7d%5d
And the response from fiddler.
HTTP/1.1 200 OK
Server: ASP.NET Development Server/11.0.0.0
Date: Fri, 15 Feb 2013 23:18:58 GMT
X-AspNet-Version: 4.0.30319
Transfer-Encoding: chunked
Cache-Control: private
Content-Type: text/html; charset=utf-8
Connection: Close

150
[{"jsonrpc":"2.0","result":{"myparam":"myvalue","myOtherParam":7},"id":2},
{"jsonrpc":"2.0","result":"Echo This","id":2},{"jsonrpc":"2.0","result":"::1","id":3},
{"jsonrpc":"2.0","result":"Mozilla/5.0 (Windows NT; Windows NT 6.2; en-US) WindowsPowerShell/3.0","id":4},
{"jsonrpc":"2.0","result":"2013-02-15T16:18:58.9527533-07:00","id":5}]
0

Notice, that to support batches, you place all your JsonRpc objects inside an array '[]' separated by comma ','.

Good luck!

-Austin