球衣客户总是抛出400错误请求

时间:2019-01-14 15:35:43

标签: java jersey-client

我有休息电话。每当我尝试使用邮递员打时,它都可以正常工作。但是如果我尝试使用JerseyAPI Client进行相同的帖子调用。我收到400个错误的请求

字符串URI =“ rest uri”;

    Client client = Client.create();

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

        String input1 = "{\"callType\": \"UPDATE\",\"emails\": [\"qa_tester2222@gmail.com\",\"qa_tester2222@gmail.com\"],\"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();

    }

}

如果我使用上述输入在Postman中运行相同的请求,则可以成功命中api并获得200,但在JerseyAPI中获得400

1 个答案:

答案 0 :(得分:-1)

由于收到错误请求响应,我认为您应该使用适当的JSON工具创建JSON字符串。您可以从字符串创建JSON对象开始,然后使用JSON对象检索输入。

创建JSON对象:

JSONObject jsonObject = new JSONObject(string);

使用JSON对象作为输入:

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