ddfra
ddfra

Reputation: 2565

Java http request for django rest framework service throws http 406 exception

I hava a django rest framework web service that works fine with httpie and firefox: when I request with httpie I have a json formatted answer and when I request with firefox an html formatted one (httpie is a http client). Now I'm building java API to communicate with services. I'm using URL class to perform requests. I can receive html-formatted answers from the server if I don't override the content-type property. So I looked how httpie overrides this property and did the same:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
connection.setRequestProperty("Accept", "*\\*");

Now the communication end with Http 406 error, which means that client can't accept the answer.

If I use only the content-type property I have no error but still the html-formatted answer

Does anyone know how to solve it?

EDIT (adding requests' header):

httpie: GET /match/39.3280114/16.241917599999965/0/5/ HTTP/1.1 Host: 127.0.0.1:8001 Connection: keep-alive Accept-Encoding: gzip, deflate Accept: / User-Agent: HTTPie/0.9.3

java-API GET /match/39.3280114/16.241917599999965/0/5/ HTTP/1.1 Host: 127.0.0.1:8001 Accept-Encoding: gzip, deflate Accept: ** User-Agent: Java-API

Solved: I was using the wrong slash for Accept property

Upvotes: 2

Views: 958

Answers (1)

Vasiliy Faronov
Vasiliy Faronov

Reputation: 12310

Your Accept header is malformed. It should be:

Accept: */*

See RFC 7231 § 5.3.2.

However, */* means “any media type.” If you actually want a specific media type (JSON), you should request it:

Accept: application/json

Upvotes: 1

Related Questions