0

I want to get some data from the server according to my auth-token.

According to the REST principals i need ti use GET method.

but i don't want to send the user auth-token in the request url.

How can i add a json to the httpGet instance?

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        HttpGet httpGet = new HttpGet(url);

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpGet.setEntity(se);

httpGet.setEntity throws an error

The method setEntity(StringEntity) is undefined for the type HttpGet

Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
  • The method setEntity(StringEntity) is undefined for the type HttpGet – Elad Benda2 Apr 13 '14 at 09:33
  • you can send `json` with `httpPost`, you can't post json with `httpGet` – Shayan Pourvatan Apr 13 '14 at 09:34
  • Oh, right. Get method has not any body. you should do all on your url! like "http://stackoverflow.com/name=Hossein&value=Mobasher" so, your json should convert to url encoded string. – Hossein Mobasher Apr 13 '14 at 09:35
  • then in my case using rest you would suggest using POST +json **or** GET with quesryString ? after all i want to get data from the server – Elad Benda2 Apr 13 '14 at 09:35
  • see http://stackoverflow.com/questions/6073896/sending-json-object-to-the-server-via-http-get – Shayan Pourvatan Apr 13 '14 at 09:35
  • @HosseinMobasher but i recall from my times in web developing sending body with get is possible. for cases just like this – Elad Benda2 Apr 13 '14 at 09:36
  • Based on "http://stackoverflow.com/a/983458/860721" : **"You can send a request body with GET but it should not have any meaning. If you give it meaning by parsing it on the server and changing your response based on its contents you're violating the HTTP/1.1 spec."** – Hossein Mobasher Apr 13 '14 at 09:40
  • You can also take a look at "http://stackoverflow.com/a/12535806/860721" – Hossein Mobasher Apr 13 '14 at 09:46
  • thanks, then why would i send `body if it has not have any meaning`? so in that case when i need t **GET** data from the server but don't want to expose my authToken i should use **POST** ? – Elad Benda2 Apr 13 '14 at 09:46
  • If you want to get anything from server, you should use `GET` method (with note that ReST!). You can add your json to url with url encoder methods. – Hossein Mobasher Apr 13 '14 at 09:50

0 Answers0