Post Data using Jersey

时间:2019-04-04 05:32:43

标签: jersey

I am trying to make a request to a 3rd paty that is equivalent to the following:

curl -G -X GET -u {API KEY}:X https://some.com/api/head/remotelogin.json -d 'id=1234&msg=hello'

The id is also used by third party to validate the request.

Below is the interface that I wrote:

public interface ThirdParty Client {
    @POST
    @Path("api/head/remotelogin.json")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumer(MediaType.APPLICATION_FORM_URLENCODED)
    Response authenticate(@HeaderParam(HttpHeaders.AUTHORIZATION) String authHeader, String payload)
}

I am not sure how to build the payload, and am I using @Consumer(MediaType.APPLICATION_FORM_URLENCODED) correctly?

I am doing my post request in Java like

final Response response = ThirdPartyClient.authenticate(base64encoded("{API KEY}:X"), "id=1234&msg=hello");

I am getting 200, but the id is wrong.

I am fairly new to Jersey, and I spent the entire afternoon reading the docs and trying things out.

1 个答案:

答案 0 :(得分:0)

睡一觉后,答案是...

    @GET
    @Path("api/head/remotelogin.json")
    @Produces(MediaType.APPLICATION_JSON)
    Response authenticate(@HeaderParam(HttpHeaders.AUTHORIZATION) String authHeader, @QueryParam("id") String id, @QueryParam("msg") String msg);

final Response response = ThirdPartyClient.authenticate("Basic " + base64encoded("{API KEY}:X"), "1234", "hello");

"Basic "在这里至关重要。