Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing JSON data in get request as request body

Hi i have to send a get request to an url
http://onemoredemo.appspot.com/group?authToken=access_token&authMethod=oauth

with request body contains json object as shown below.

{"goupid":"some_variable"
}

Here is a section of java code for sending get request:

URL url1=new URL("http://onemoredemo.appspot.com/group?authToken="+access_token+"&authMethod=oauth");
conn=(HttpURLConnection) url1.openConnection();
conn.addRequestProperty("Content-type", "application/x-www-form-urlencoded");

conn.setRequestMethod("GET");
conn.setDoOutput(true);
JSONObject jj=new JSONObject();
HttpGet get;
get.

jj.put("groupid", "[email protected]");
conn.addRequestProperty("Content-TYpe", "application/json");
conn.getOutputStream().write(jj.toString().getBytes());
conn.connect();
InputStream is=conn.getInputStream();

I am getting an error java.io.FileNotFoundException.

I sent a request from mozilla browser to url
http://onemoredemo.appspot.com/group?authToken=ya29.AHES6ZRDl-RqiA8W0PhybU_hMluHrHRjlJBvq06Vze0izJq0Ovjc088&authMethod=oauth
It was giving me correct response but now its more than one hour so acccesstoken expire. I know its weird to send parameter as well as requestbody in get request but i have to send it.

Please help in how to send a json object in request body in get request.

like image 891
Ritesh Mehandiratta Avatar asked Jul 20 '12 08:07

Ritesh Mehandiratta


People also ask

Can I send JSON data in GET request?

To get JSON from a REST API endpoint, you must send an HTTP GET request and pass the "Accept: application/json" request header to the server, which will tell the server that the client expects JSON in response.

How do you send JSON in the request body?

Set the Request Content-Type Header Parameter. Set the “content-type” request header to “application/json” to send the request content in JSON form. This parameter has to be set to send the request body in JSON format.

Can we use request body for GET method?

Requests using GET should only be used to request data (they shouldn't include data). Note: Sending body/payload in a GET request may cause some existing implementations to reject the request — while not prohibited by the specification, the semantics are undefined.


2 Answers

Don't do it.

Read this: http://tech.groups.yahoo.com/group/rest-discuss/message/9962

"Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

So, yes, you can send a body with GET, and no, it is never useful to do so.

This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress)."

For other interesting discussions on this check this:

https://stackoverflow.com/a/978094/550967

https://stackoverflow.com/a/978173/550967

https://stackoverflow.com/a/978519/550967

like image 126
DallaRosa Avatar answered Sep 30 '22 09:09

DallaRosa


The body of a GET request is not read.

Have you tried adding it to the params:

http://onemoredemo.appspot.com/group?authToken=access_token&authMethod=oauth&goupid=some_variable

like image 42
Blundell Avatar answered Sep 30 '22 10:09

Blundell