Search code examples
jersey

Post Data using 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.


Solution

  • After getting some sleep, the answer is...

        @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");

    The "Basic " is essential in here.