0

Our mobile application uses a POST request to check its status. We need to migrate this service to new location. We have released new version of the mobile application using new endpoint. And I planned to release update of the service that redirects the client to new location.

@RequestMapping(value = "/url", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public void performHandShake(HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", service.getNewUrl());
}

I am testing this service with Postman and current Android implementation and they both fail on HTTP error 405 - method not allowed. It seems that they both redirect to GET instead of requested POST. Is there any proper way to redirect the POST request? I do not want to implement a proxy to the new endpoint.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156

1 Answers1

2

Use 308 instead of 301 if you don't want the have the request method changed from POST to GET:

6.4.2. 301 Moved Permanently

The 301 (Moved Permanently) status code indicates that the target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. [...]

Note: For historical reasons, a user agent MAY change the request method from POST to GET for the subsequent request. If this behavior is undesired, the 307 (Temporary Redirect) status code can be used instead.

3. 308 Permanent Redirect

The 308 (Permanent Redirect) status code indicates that the target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. Clients with link editing capabilities ought to automatically re-link references to the effective request URI to one or more of the new references sent by the server, where possible. [...]

Note: This status code is similar to 301 (Moved Permanently), except that it does not allow changing the request method from POST to GET.

See this answer for more details.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Nice. Unfortunatelly currently used library (com.squareup.retrofit2:retrofit:2.1.0) does not support this code. And because I need a solution for existing customers that do not upgrade I cannot change the library. – Leos Literak Apr 04 '18 at 15:21
  • @LeosLiterak Well, you could try [`307` (Temporary Redirect)](https://tools.ietf.org/html/rfc7231#section-6.4.7). – cassiomolin Apr 04 '18 at 15:31