Thursday, July 17, 2014

Example of HTTP POST and PUT using RESTful interface with JSON data being sent and returned.

Here is what is sent over the wire when you HTTP POST or PUT JSON data via a RESTful (like) API.   The example has been mocked up using Python and Flask.  The goal of this exercise was to show how the data is transmitted as well as all the HTTP headers in use for the transaction.   




Command to initiate the POST:
curl -i -H "Content-Type: application/json" -X POST -d '{"variable1":"data1", "variable2":"data2"}' http://localhost:5000/api/2

Packet capture of POST using Wireshark to follow the TCP stream:
POST /api/2 HTTP/1.1
User-Agent: curl/7.30.0
Host: localhost:5000
Accept: */*
Content-Type: application/json
Content-Length: 42
{"variable1":"data1", "variable2":"data2"}

Packet capture of data returned from POST:
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 96
Server: Werkzeug/0.9.6 Python/2.7.5
Date: Fri, 18 Jul 2014 01:10:51 GMT
{
  "post_id": 2,
  "variable1": "data1",
  "variable2": "data2"
}




Command to initiate the PUT:
curl -i -H "Content-Type: application/json" -X PUT -d '{"variable3":"data3", "variable4":"data4"}' http://localhost:5000/api/3

Packet capture of PUT using Wireshark to follow the TCP stream:
PUT /api/3 HTTP/1.1
User-Agent: curl/7.30.0
Host: localhost:5000
Accept: */*
Content-Type: application/json
Content-Length: 42
{"variable3":"data3", "variable4":"data4"}

Packet capture of data returned from PUT:
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 68
Server: Werkzeug/0.9.6 Python/2.7.5
Date: Fri, 18 Jul 2014 01:17:39 GMT
{
  "post_id": 3,
  "variable3": "data3",
  "variable4": "data4"
}

You don't have to use CURL to send the data.  Here is an example of using telnet instead.  After you connect, paste in the data starting at the PUT and ending at the {...}:

jemurray@dsg:~ $ telnet localhost 5000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
PUT /api/3 HTTP/1.1
User-Agent: telnet/0.1
Host: localhost:5000
Accept: */*
Content-Type: application/json
Content-Length: 42
{"variable3":"data3", "variable4":"data4"} 


No comments:

Post a Comment

Followers