0
votes

I have a rest post call. whenever I try to hit using postman it is working fine. but If I try same post call using JerseyAPI Client. I'm getting 400 Bad request

String URI = "rest uri";

    Client client = Client.create();

    WebResource webResource = client.resource(URI);
    try {

        String input1 = "{\"callType\": \"UPDATE\",\"emails\": [\"[email protected]\",\"[email protected]\"],\"event\": \"UPDATE_EVENT\",\"externalIds\": [ \"id\" ], \"fraudAction\": \"CONFIRMED_FRAUD\",\"fraudCategory\": \"Account Takeover\",\"memo\": \"test fraud case management for AD\",\"origin\": \"SE4\",\"phones\": [],\"requester\": \"corsairUser\",\"source\": \"LIVE\" }";

        ClientResponse response = webResource.type("application/json")
                .header("Authorization", "Basic secretKey")
                .post(ClientResponse.class, input1);

        if (response.getStatus() != 201) {
            throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }

        System.out.println("Output from Server .... \n");
        String output = response.getEntity(String.class);
        System.out.println(output);

    } catch (

    Exception e) {

        e.printStackTrace();

    }

}

If I run same request in Postman using a above input, I'm able to hit api successfully and get 200, but getting 400 in JerseyAPI

1
Is postman sending any different headers? Are you sure the JSON data is identical/valid? Does the server return any useful hints in the body?f1sh
Could you add the stacktrace that is being printed?Fabian Rivera

1 Answers

-1
votes

Since you are getting a Bad Request response, I think you should create your JSON string using a proper JSON tool. You can start by creating a JSON object from your string and then use the JSON object to retrieve the input.

Creating the JSON object:

JSONObject jsonObject = new JSONObject(string);

Using the JSON object as your input:

ClientResponse response = webResource.type("application/json")
                .header("Authorization", "Basic secretKey")
                .post(ClientResponse.class, jsonObject.toString());