Warpzit
Warpzit

Reputation: 28152

Why can't I set header with Android HTTPURLConnection?

So I'm trying to connect to a REST server with HTTPURLConnection. The HTTPGET request needs to be of content-type application/json. When I use setRequestProperty("Content-Type", "application/json"); the value is overriden to "text/html" but when I use setRequestProperty("Accept", "application/json"); the Content-Type is set to application/json. Why can't I use Content-Type when specifying? Clarification is greatly appreciated.

My current guess is that "Accept" works with HTTPGET and "Content-Type" is for HTTPPOST.

More code:

connection.setRequestMethod("GET");
connection.setUseCaches(false);
//connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");

Upvotes: 0

Views: 398

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

AFAIK, the Content-Type header in an HTTP request would be for the type of content being sent as the body, and is used on requests like POST and PUT. Accept is the header that indicates what MIME types the requester would like to receive in a response.

Upvotes: 1

Related Questions