POST API not working on Heroku when accessed via own domain

Viewed 81

I have a Spring Boot app running on Heroku and I am facing a weird problem.

If I try to access https://appname.herokuapp.com/authentication/token which is a POST request it works as expected with no problem.

However, I have configured my DNS and Heroku app to work on myxyzappdomain.com too and for most part it does. I can access the index.html page and all GET APIs. But, If I try to access any POST API like the one above (https://myxyzappdomain.com/authentication/token) I get a 405 error -

2020-08-17T19:47:31.039889+00:00 heroku[router]: at=info method=GET path="/authentication/token" host=www.myxyzappdomain.com request_id=92dbc99e-ed32-4643-abdb-7a0a8b6828f1 fwd="27.56.186.133,141.101.98.19" dyno=web.1 connect=6ms service=17ms status=405 bytes=287 protocol=http
2020-08-17T19:47:31.028327+00:00 app[web.1]: 2020-08-17 19:47:31.028 DEBUG 4 --- [io-57518-exec-6] o.s.web.servlet.DispatcherServlet        : GET "/authentication/token", parameters={}
2020-08-17T19:47:31.031695+00:00 app[web.1]: 2020-08-17 19:47:31.031 DEBUG 4 --- [io-57518-exec-6] .m.m.a.ExceptionHandlerExceptionResolver : Using @ExceptionHandler com.abc.xyz.globalexceptions.GlobalExceptionHandler#handleException(Exception, WebRequest)
2020-08-17T19:47:31.031869+00:00 app[web.1]: 2020-08-17 19:47:31.031  WARN 4 --- [io-57518-exec-6] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported
2020-08-17T19:47:31.033581+00:00 app[web.1]: 2020-08-17 19:47:31.033 DEBUG 4 --- [io-57518-exec-6] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : No match for [*/*], supported: []
2020-08-17T19:47:31.037345+00:00 app[web.1]: 2020-08-17 19:47:31.037 DEBUG 4 --- [io-57518-exec-6] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported]
2020-08-17T19:47:31.037484+00:00 app[web.1]: 2020-08-17 19:47:31.037 DEBUG 4 --- [io-57518-exec-6] o.s.web.servlet.DispatcherServlet        : Completed 405 METHOD_NOT_ALLOWED

Notice how it is trying to accept the POST request as a GET one and failing to do so. What is happening here?

1 Answers

It would appear you have a "middle-man" (according to your comments: a CloudFlare configuration) that redirects from the naked domain myxyzappdomain.com to www.myxyzappdomain.com.

You wrote that you use the URL https://myxyzappdomain.com/authentication/token, yet in your log output it says host=www.myxyzappdomain.com (note the www). That means that a redirect has taken place, probably using status code 302. Although this was originally not defined this way in the HTTP spec, nowadays virtually all clients change the request method to GET when a 302 redirect is received from a POST request.

For entering a URL in the browser this is not a problem, because it will be a GET request in the first place, but for API usage it is.

To avoid this issue, you can send your POST request to the final URL of the redirect in the first place - to https://www.myxyzappdomain.com/authentication/token (with www), so there won't be any redirect messing with your HTTP method.

Related