1

I'm developing an Android application which communicates with (our own) API. It was meant to use the API in manipulative requests with the request method POST and at not-manipulative requests GET (as it should be in RESTful applications).

To authenticate or add parameters to the request, the HTTP request body has been used (in both GET and POST requests). (YES, it is possible and allowed to add a request body to GET requests per HTTP definition (see e.g. this post)). The post generally says, that it is possible to add a request body, but the server may not use it during the request.

The problem is, that the request method is always set to POST, no mather if I set it to GET anywhere during the connection configuration, even if the getRequestMethod does return GET after setting it to GET via setRequestMethod("GET").

The android application uses the HttpsURLConnection (which is an extended class from HttpURLConnection, so it should behave similary).

By calling these methods, a request body will be attended:

https.setDoInput(true);
OutputStream os = https.getOutputStream();
os.write(outputInBytes);
os.close();

And by calling https.setRequestMethod("GET"), the request method should be set to GET.

After a little investigating, the line OutputStream os = https.getOutputStream(); sets the request method to POST, afterwards I set it to GET again and it remains GET till the end of the connection (as returned by https.getRequestMethod())

But in the end the server receives the request with the request method POST.

So my specific questions are:

  • Is there a possible workaround / solution for this problem?
  • Is it really that bad to add a request body to a GET request?

Currently I have just set all requests to POST, so there is no problem with it (and I wouldn't have a problem to leave it this way, but for several reasons I would like to know for sure that there is no other way to fix this problem)

Edit: The documentation of the getOutputStream() method says:

The default request method changes to "POST" when this method is called.

Community
  • 1
  • 1
Florian Mötz
  • 95
  • 2
  • 5

1 Answers1

1

By default the HttpURLConnection is a GET Method (getDoInput() is true by default).

If you use setDoOutput(true) it will become a POST method.

If you need another method (PUT, DELETE, etc...) then you will use setRequestMethod(string).

And of course you have to select the method you want before the connect() method

xiaomi
  • 6,269
  • 3
  • 27
  • 34
  • Thanks for your reply, but I am currently setting the request-method to GET _before_ the `connect()` method. The `getRequestMethod` then does return **GET**, but the request is still performed with POST (which is my problem) – Florian Mötz Feb 28 '16 at 16:44
  • If you are sending data, this will be considered like a post. a GET Method should not send information. The query should be into the url or by adding request properties into the header. Here I see an outputInBytes, what is it ? – xiaomi Feb 28 '16 at 16:48
  • I do get that it is common to send parameters as GET parameters or in the header fields, but [following this post (same link as in original post)](http://stackoverflow.com/a/983458/5045290), it is allowed per RFC to include a request body to a GET request. But I think it will be better to not send a GET request body because not many clients/server do understand that it is possible.. – Florian Mötz Feb 28 '16 at 16:56
  • When I send my request, I use for example `https.setRequestProperty("Authorization", "code=" + code);` – xiaomi Feb 28 '16 at 17:06
  • Ok thanks, that means I will use header fields for authorization and other data that needs to be sent to the server in GET response in further projects. However I will leave this question un-answered for some time, if someone is able to post a workaround for the respone-body-GET problem... :) – Florian Mötz Feb 28 '16 at 17:18