且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 中的 PUT 请求

更新时间:2023-09-11 23:20:04

我知道这不是您为什么会收到错误 input_data value not provided 的确切答案.主要是因为代码似乎不必要地复杂.此外,很难说发生了什么,尤其是当您在 URL 处调用的端点如何处理请求很清楚时.

I know this is not the exact answer to the question on why you are getting the error input_data value not provided. Mainly because code seems to be unnecessarily complex. Also it's difficult to say what's going on, especially when it's clear how the request is handled by the endpoint which you are calling at URL.

如果您使用的是 Java 11,则可以使用 JDK 提供的简化代码 HttpClient 并且还应该在失败时为您提供更清晰的错误.

If you are using Java 11, you can simplify the code using JDK provided HttpClient and should also give provide you more clear error when it fails.

以下是您可以在您的方法中尝试的代码摘录

Below is a code excerpt you can try in your method

String url = "YOUR URL";
String body = "{" +
            "    \"request\": {" +
            "        \"status\": {" +
            "            \"name\": \"Closed\"" +
            "        }" +
            "    }" +
            "}";

String data = "\"input_data\": "+ body;
HttpRequest request = HttpRequest.newBuilder()
            .PUT(HttpRequest.BodyPublishers.ofString(data))
            .uri(URI.create(url))
            .setHeader("admin_key", "0fdgdf0g64fr4h4e24z4df5xv")
            .setHeader("Content-Type", "application/json")
            .build();

var client = HttpClient.newHttpClient();

var response = client.send(request, HttpResponse.BodyHandlers.ofString());

System.out.println(response.statusCode());
System.out.println(response.body());