Java 使用 Restlet 通过 POST 发送 JSON 导致“400 Bad request”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20720955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:48:01  来源:igfitidea点击:

Sending JSON by POST using Restlet results in "400 Bad request" error

javajsonpostrestlet

提问by Danilo Radenovic

When trying to send a JSON objectto the server using POSTmethod, "400 Bad request" error is returned. Standard Java application (SDK) with Restlet api is used to make this request.

尝试使用POST方法将JSON 对象发送到服务器时,返回“400 Bad request”错误。带有 Restlet api 的标准 Java 应用程序 (SDK) 用于发出此请求。

Given that the:

鉴于:

here is how the code so far looks like:

到目前为止,代码如下所示:

ClientResource resource = new ClientResource("http://myhostname.com/api/v1/parts/");

        resource.setMethod(Method.POST);
        resource.getReference().addQueryParameter("format", "json");
        resource.getReference().addQueryParameter("access_key", "8QON4KC7BMAYYBCEX");

            // create json object and populate it
            JSONObject obj = new JSONObject();
        try {
               obj.put("partId", "23");
               obj.put("carId", "34");
               obj.put("name", "chassis");
               obj.put("section", "frame");
        } catch (JSONException e1) {// handling of exception}

        StringRepresentation stringRep = new StringRepresentation(obj.toString());
        stringRep.setMediaType(MediaType.APPLICATION_JSON);

        try {
               resource.post(stringRep).write(System.out); // exception occurs here
        } catch (Exception e) {// handling of exceptions }

Response from server is "400 Bad request". Here is the console output:

来自服务器的响应是“400 Bad request”。这是控制台输出:

Bad Request (400) - BAD REQUEST
    at org.restlet.resource.ClientResource.doError(ClientResource.java:612)
    at org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1202)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1069)
    at org.restlet.resource.ClientResource.handle(ClientResource.java:1044)
    at org.restlet.resource.ClientResource.post(ClientResource.java:1453)
    at tests.RESTTestReceiverPOST.main(RESTTestReceiverPOST.java:39)

When using Chrome POSTMANplugin to send this json object (and it works with POSTMAN), the output of the POST request looks like this:

当使用 Chrome POSTMAN插件发送这个 json 对象(它与 POSTMAN 一起工作)时,POST 请求的输出如下所示:

/api/v1/parts/?format=json&access_key=8QON4KC7BMAYYBCEX HTTP/1.1
Host: myhostname.com
Content-Type: application/json

{ "partId": "23", "carId": "34", "name": "chassis", "section": "frame" }

Any suggestions on what may be wrong in the code? Thanks.

关于代码中可能有什么问题的任何建议?谢谢。

采纳答案by Danilo Radenovic

Solved it. You were right @Octopus, there was a problem in this code:

解决了。你是对的@Octopus,这段代码有问题:

   obj.put("partId", "23");
   obj.put("carId", "34");
   obj.put("name", "chassis");
   obj.put("section", "frame");

It should look like (numbers without quotes):

它应该看起来像(没有引号的数字):

obj.put("partId", 23);
obj.put("carId", 34);
obj.put("name", "chassis");
obj.put("section", "frame");

Thanks again both @Octopus and @Sotirios Delimanolis for your time and help.

再次感谢@Octopus 和@Sotirios Delimanolis 的时间和帮助。