Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send raw data in an HTTP GET request?

In the example at http://alx3apps.appspot.com/jsonrpc_example/ when I click the submit button, I notice (by using Firebug) that my browser submits the source:

{"params":["Hello ","Python!"],"method":"concat","id":1}

It's not posting a parameter (eg. json=[encoded string from above]), but rather just posting a raw string with the above value.

Is there an widely accepted way to replicated this via a GET request, or do I need to just urlencode the same string and include it as http://www.example.com/?json=%7b%22params%22%3a%5b%22Hello+%22%2c%22Python!%22%5d%2c%22method%22%3a%22concat%22%2c%22id%22%3a1%7d? I understand that some older browsers cannot handle a URI of more than 250 characters, but I'm OK with that.

like image 668
orokusaki Avatar asked Dec 16 '10 17:12

orokusaki


People also ask

Can you send data with HTTP GET?

Can I send data using the HTTP GET method? No, HTTP GET requests cannot have a message body. But you still can send data to the server using the URL parameters. In this case, you are limited to the maximum size of the URL, which is about 2000 characters (depends on the browser).

Can we send data in GET method?

The GET method is the method used by the browser to ask the server to send back a given resource: "Hey server, I want to get this resource." In this case, the browser sends an empty body. Because the body is empty, if a form is sent using this method the data sent to the server is appended to the URL.

What is raw body in HTTP request?

Raw body content refers to the ability for you to document APIs that have no named JSON parameters. Something like the following in cURL: cURL. curl --request POST \ --url https://httpbin.org/post \ --header 'content-type: application/json' \ --data 'string' This also works for other primitives, arrays and objects.


2 Answers

A GET request doesn't usually transmit data in any other way besides headers, so you should pass the string encoded in the URL if you wish to use GET.

POST http://alx3apps.appspot.com/jsonrpc_example/json_service/ HTTP/1.1
Host: alx3apps.appspot.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/json-rpc; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://alx3apps.appspot.com/jsonrpc_example/
Content-Length: 55
Pragma: no-cache
Cache-Control: no-cache

{"params":["Howdy","Python!"],"method":"concat","id":1}

In a normal form post the header Content-Type: application/x-www-form-urlencoded lets the server know to expect the format in key=val format whereas the page you linked sends Content-Type: application/json-rpc; charset=UTF-8. After the headers (which are terminated with the blank line) the data follows in the specified format.

like image 115
Deebster Avatar answered Sep 23 '22 17:09

Deebster


You are correct that only POST submits data separately from the URI. So urlencoding it into the querystring is the only way to go, if you must use GET. (Well, I suppose you could try setting custom request headers or using cookies, but the only "widely accepted" way is to use the querystring.)

like image 23
Domenic Avatar answered Sep 24 '22 17:09

Domenic