POST 请求 Jersey 到 API 时出错,与 POSTMAN 一起工作正常

Error on POST request with Jersey to API, works fine with POSTMAN

我用 Laravel 5 开发了一个 API RESTful 服务(后端),我用 POSTMAN 测试了它。有多种资源可以通过 GET 和 POST 请求获取信息,它们可以与 POSTMAN 一起正常工作。我开始使用带有 Jersey 的 Java 客户端测试 API。 GET 方法可以很好地处理这样的代码,响应是 json,我用 Jackson 对其进行了解析。

Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(ClientUrl);
Invocation.Builder invocationBuilder = 
webTarget.request(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
Response mResponse = invocationBuilder.get();

我要测试的下一个方法是登录,使用 POST 方法。 对于 POSTMAN,我在 header 中使用键 "Content-Type" + 值 "application/x-www-form-urlencoded",在 body 中,我发送一个包含此信息的 Json

{"email":"xxxxxx@gmail.com", "password":"xxxxxxxxxx"}

要对 Jersey 使用 POST 方法,我使用下一个代码

Client client = ClientBuilder.newClient();
Form mForm = new Form();
mForm.param("email", email);
mForm.param("password", password);

WebTarget target = client.target(Utils.URL_LOGIN);
//As stated in the documentation, the APPLICATION_FORM_URLENCODED = 
//"application/x-www-form-urlencoded"
Builder request = target.request(MediaType.APPLICATION_FORM_URLENCODED);
//I print the request to verify the request 
print(request.toString());
Response response =  request.post(Entity.form(mForm));
//I print the response to verify the response received and also the code 
print("Response: " + response.toString());
print("Code: " + response.getStatus());
String result = response.readEntity(String.class);
response.close();
print(result);

当我打印 request.toString() 时,我得到这个

org.glassfish.jersey.client.JerseyInvocation$Builder@65e579dc

打印响应时

Response: InboundJaxrsResponse{context=ClientResponse{method=POST, uri=http://www.dance-world.com/api/login, status=200, reason=OK}}

收到的状态是 200,但是当我打印 response.readEntity 时,我得到了我在后端使用的响应,用于那些服务器没有收到请求信息的情况。

{"code":400,"status":"error","message":"No data attached to the request"}

当我使用 POSTMAN 并且结果为 200 时,我成功地收到了字符串形式的令牌。

我不知道我的错误是什么,因为我正在按照文档中的代码和 RESTful Java 一书以及 JAX-RS 2.0

我也尝试过使用 Unirest,因为它比 Jersey 更容易使用。我输入了下面的代码

 try {
      HttpRequestWithBody requestwithBody = Unirest.post(url)
      .header("Content-Type", "application/json")
      .header("accept", "application/json");

      RequestBodyEntity requestBodyEntity = requestwithBody.body(input);

      response = requestBodyEntity.asJson();

} catch (UnirestException e) {
    e.printStackTrace();
} finally {
    print("Status: " + String.valueOf(response.getStatus()));
    print("Body: " + String.valueOf(response.getBody()));
}

本地服务器没有收到 json 正文,我从服务器

收到了相同的响应
{"code":400,"status":"error","message":"No data attached to the request"}

找到原因了。我基于Laravel的课程开发了后端,在那个课程中函数有下一个定义

 public function getByCountry(Request $request) {
        //Recoger los datos por post
        $json = $request->input('json', null);
        $params = json_decode($json);
        $params_array = json_decode($json, true);
        ...
}

并且后端需要一个 x-www-urlencoded,在 POSTMAN 中,我从后端以那种格式 (POST Request {x-www-urlencoded} with Postman). So, when I tried to make a POST request with JSON, using Jersey or Unirest the request haven't the required format, that's the reason I had the message configured for thoses cases when the backend doesn't receive information with te request. This is the POSTMAN request with raw Json and this is the response 发送请求,表示没有收到数据。

我解决它以接受 json 请求的方法是用下一个结构更改后端代码:

public function TestForPost(Request $request) {
    //Verify if the $request is a json
    $input = $request->json()->all(); //read json in request
    if (!empty($input)) {
    ...
}

通过此更改,我可以发送带有 Json 的请求,并且后端会接受它。