JSON-RPC over HTTP

Date: 2008-1-15
Web site:http://groups.google.com/group/json-rpc
Author: Jeffrey Damick & JSON-RPC group - json-rpc -AT- googlegroups DOT com

Contents


1   Preface

"JSON-RPC is a lightweight remote procedure call protocol. It's designed to be simple!" [JSON-RPC 1.0]

The goal of this document is to propose a JSON-RPC 1.2 extension to specify

For mailing list location: JSON-RPC Google Group

2   Change Log

3   Specification

3.1   Overview

JSON-RPC over HTTP is an extension to the JSON-RPC 1.2 specification.

3.2   Conventions

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Since JSON-RPC uses JSON, it shares the same type system as JSON (see http://www.json.org or RFC 4627). Whenever this document refers to any JSON type, the first letter is always capitalized: Object, Array, String, Number, True, False, Null.

Clients are the origin of Request objects. Servers are the origin of Response objects.

3.3   HTTP Header

Requirements:

Regardless of whether a remote procedure call is made using HTTP GET or POST, the HTTP request message MUST specify the following headers:

  • Content-Type SHOULD be 'application/json-rpc' but MAY be 'application/json' or 'application/jsonrequest'
  • The Content-Length MUST be specified and correct according to the guidelines and rules laid out in Section 4.4, Message Length, of the HTTP specification.
  • The Accept MUST be specified and SHOULD read 'application/json-rpc' but MAY be 'application/json' or 'application/jsonrequest'.

3.4   POST

Post body contains the Object request specified in JSON-RPC 1.2 section 2.4.

Response to a Post contains the Object response specified in JSON-RPC 1.2 section 2.5.

Responses with a null error will result in an HTTP 200 Status Code, while non-null error codes will be mapped to HTTP Status code as specified in the Errors section.

--> POST /ENDPOINT HTTP/1.1
 Host: ...
 Content-Type: application/json-rpc
 Content-Length: ...

 {"method": "sum", "params": {"a":3, "b":4}, "id":0}

<-- HTTP/1.1 200 OK
 ...
 Content-Type: application/json-rpc

 {"result": 7, "error": null, "id": 0}


--> POST /ENDPOINT HTTP/1.1
 Host: ...
 Content-Type: application/json-rpc
 Content-Length: ...

 {"method": "test", "params": [], "id": 0}

3.5   GET

  • MUST be either Array or Object parameters [1]
  • Keep in mind that some old clients and proxies have issues wth URI lengths over 255 bytes [2]
  • HTTP POST is the preferred method for sending JSON-RPC
  • Parameters must be encoded
http://<end point>?method=<method name>&params=<encoded params>&id=1

3.5.1   Encoded Parameters

Encoding Steps:

  1. Base 64 param value
  2. URL Encoded Base 64 param value

Pre-Encoded Params:

http://<end point>?method=sum&params={"a":3,"b":4}&id=2
http://<end point>?method=sum&params=[3,4]&id=1

Encoded Request:

http://<end point>?method=sum&params=eyJhIjozLCJiIjo0fQ%3D%3D&id=2
http://<end point>?method=sum&params=WzMsNF0%3D&id=1

3.6   Response Codes

3.6.1   Success

For non-notification requests [3] including system procedures using POST or GET MUST indicate a success response using HTTP status code: 200.

However, for JSON-RPC 1.2 Notification requests, a success response MUST be an HTTP status code: 204.

3.6.2   Errors

HTTP Status code message
500 -32700 Parse error.
400 -32600 Invalid Request.
404 -32601 Method not found.
500 -32602 Invalid params.
500 -32603 Internal error.
500 -32099..-32000 Server error.

For more details on error codes see table in JSON-RPC 1.2 section 2.5.1.

For more details on HTTP Status codes, refer to HTTP section 6.1.1.


[1]Per JSON-RPC 1.2 specification
[2]http://classicasp.aspfaq.com/forms/what-is-the-limit-on-querystring/get/url-parameters.html
[3]JSON-RPC 1.2 section 2.4

Copyright (C) 2008 by JSON-RPC Working Group

This document and translations of it may be used to implement JSON-RPC over HTTP, it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way.

The limited permissions granted above are perpetual and will not be revoked.

This document and the information contained herein is provided "AS IS" and ALL WARRANTIES, EXPRESS OR IMPLIED are DISCLAIMED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.