web services - GET Request with Content-Type and Accept header with JAX-RS Jersey 2.2 -
i try access open data web service gives me traffic infos. documentation says requests have get , need contain accept: application/json , content-type: application/json. don't understand why need content-type ok:
i tried retrieve data accept: header i'm getting 415 unsupported media type. trying way (but i'm not sure if setting both headers correctly):
string entity = clientbuilder.newclient().target(livedatauri) .path(livedatapath) .request(mediatype.application_json) .accept(mediatype.application_json) .get(string.class); as see using jersey 2.2 , i'm still getting 415 unsupported media type.
edit
so got work don't understand why. isn't accept(mediatype.application_json) , header("content-type","application/json") same?
string responseentity = clientbuilder.newclient() .target(livedatauri) .path(livedatapath) .request(mediatype.application_json) .header("content-type", "application/json") .get(string.class);
the accept header tells server client wants in response. content-type header tells server client sends in request. 2 not same.
if server accepts application/json, must send request specifies request content:
content-type: application/json that's why edited code works.
edit
in first code use webtarget.request(mediatype... acceptedresponsetypes). parameters of method
define accepted response media types.
you using innvocation.builder.accept(mediatype... mediatypes) on result of method call. accept() adds no new header, unnecessary in first code.
you never specify content type of request. since server expects content-type header, responds 415.
Comments
Post a Comment