Ad

How To Figure Out The Required Headers For A Java REST Call?

- 1 answer

When testing with the jsonplaceholder.typicode.com site java code does not need to set any headers. Reading the json from a URLConnection works fine. However other http endpoints will return a 403 Forbidden unless a HttpURLConnection is used and the User-Agent request property is set.

Is there a way other than trial and error to figure out the required headers for a given http endpoint?

Ad

Answer

There's nothing in the HTTP protocol that allows you to observe what headers a particular server is expecting. You send a request, and the server sends a response, that's all. (A nice server may, of course, choose to embed a helpful error message in its response.)

So the literal answer to your question is: no, there's no way to determine this beyond trial and error (unless you have access to documentation and/or source code).

Ad
source: stackoverflow.com
Ad