2

According to the void setRequestMethod (String method) documentation here:

https://developer.android.com/reference/java/net/HttpURLConnection.html#setRequestMethod%28java.lang.String%29

The allowed methods are limited to:

  • GET
  • POST
  • HEAD
  • OPTIONS
  • PUT
  • DELETE
  • TRACE

I have a server offering access to a list of messages and one of the options I have is to retrieve the next message:

NEXT /api/1/message

However, when I try to put NEXT as the method in the JavaScript HttpURLConnection I get an error:

E/MsgProcessingService: Failed while handling a message: java.net.ProtocolException: Unknown method 'NEXT'; must be one of [OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE]

Is there a non-hacky way to bypass that method limitation?

Obviously, I can create a new access point as follow on my server, but I was hoping I would not have to do that...

GET /api/1/message/next
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • 1
    There must be a good reason why they did not allow NEXT, I would imagine. – Doug Barbieri Sep 07 '17 at 16:34
  • 1
    What version of the HTTP specification defines a `NEXT` method? – CommonsWare Sep 07 '17 at 16:35
  • @CommonsWare WebDAV adds many other methods, why wouldn't you be able to do that with any type of web extension?! – Alexis Wilke Sep 07 '17 at 16:39
  • To access a WebDAV server with WebDAV methods, you would use a WebDAV client. – CommonsWare Sep 07 '17 at 16:39
  • 1
    @CommonsWare As you can see here: https://tools.ietf.org/html/rfc2616#section-5.1.1 the method can be any valid HTTP token. Therefore `NEXT` is perfectly legal. – Alexis Wilke Sep 07 '17 at 16:47
  • 1
    "Legal" does not mean that all client libraries will necessarily support it. See if there is a more forgiving HTTP client stack (e.g., OkHttp, a standalone Apache HttpClient library). – CommonsWare Sep 07 '17 at 16:51
  • @CommonsWare My problem is the client on the Android, not the server which works just fine. I have tested with curl on the command line and in PHP and I can send the `NEXT` method and I get my JSON as expected. What is completely broken is the Android function `setRequestMethod()`... – Alexis Wilke Sep 07 '17 at 16:55
  • Hence, [as I wrote](https://stackoverflow.com/questions/46101229/how-do-i-use-my-own-method-with-an-httpurlconnection-object-on-android?noredirect=1#comment79165628_46101229), see if there is a more forgiving HTTP client stack (e.g., OkHttp, a standalone Apache HttpClient library). This has nothing to do with Android. `HttpURLConnection` has this limitation in standard Java, as you can see from [the source code to the Java 8 edition of the class](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/net/HttpURLConnection.java?av=f#314). – CommonsWare Sep 07 '17 at 17:06

1 Answers1

5

No.

Those "methods" are defined in the HTTP spec. This method checks the String against a constant array.

As the spec says, these methods can be expanded, but it's very rare. The Heroku docs discuss this.

Related:

jordanpg
  • 6,386
  • 4
  • 46
  • 70
  • And yet the [HTTP documentation](https://tools.ietf.org/html/rfc2616#section-5.1.1) allows for any extension... I can use any method I want in `wget`, `curl`, JavaScript... It's sad. Oh well. – Alexis Wilke Sep 07 '17 at 16:52
  • 1
    Your question is about `HttpURLConnection`. Afaik, it's not possible with that class. There are certainly ways to do it, although I can't speak to what's available in the Android SDK. The overwhelming recommendation seems to be not to do it. Going against conventions is an extremely high risk choice, particularly for maintainability. – jordanpg Sep 07 '17 at 16:58