GET Request with Content-Type and Accept header with JAX-RS Jersey 2.2

前端 未结 3 1387
盖世英雄少女心
盖世英雄少女心 2020-12-24 15:11

I try to access an open data web service which gives me traffic infos. Documentation says that requests have to be GET and need to contain Accept: applica

3条回答
  •  悲哀的现实
    2020-12-24 15:32

    The Accept header tells the server what your client wants in the response. The Content-Type header tells the server what the client sends in the request. So the two are not the same.

    If the server only accepts application/json, you must send a request that specifies the request content:

    Content-Type: application/json
    

    That's why your edited code works.

    Edit

    In your first code you use WebTarget.request(MediaType... acceptedResponseTypes). The parameters of this method

    define the accepted response media types.

    You are using Innvocation.Builder.accept(MediaType... mediaTypes) on the result of this method call. But accept() adds no new header, it is unnecessary in your first code.

    You never specify the content type of your request. Since the server expects a Content-Type header, it responds with 415.

提交回复
热议问题