8

I have read some relative questions, but unfortunately they do not answer my one, since I have specific requirements.

Maybe it's a dumb question, but how can I request (GET) a JSON-response using httpURLConnection and the http-header "Accept"?

I found a snippet in the documentation, but I'm not sure how to do it though.

Accept = "Accept" ":" #( media-range [ accept-params ] )

1 Answer 1

13

I can't see what programming language you're talking about, so I assume it's Java since this is the first thing that pops up when searching for httpURLConnection.

If that's the case, then you can write

URL url = new URL("https://stackoverflow.com");
HttpURLConnection urlConnection = (HttpURLConnection)  url.openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
try {
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    ...
} finally {
    urlConnection.disconnect();
}

Source

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.