简体   繁体   English

如何将请求有效负载发送到Java中的REST API?

[英]How to send Request payload to REST API in java?

I want to retrieve the JSON data from the following: https://git.eclipse.org/r/#/c/11376/ 我想从以下内容中检索JSON数据: https//git.eclipse.org/r/#/c/11376/

Request URL: https://git.eclipse.org/r/gerrit/rpc/ChangeDetailService 请求URL: https://git.eclipse.org/r/gerrit/rpc/ChangeDetailServicehttps://git.eclipse.org/r/gerrit/rpc/ChangeDetailService

Request Method: POST 请求方法: POST

Request Headers: 请求标题:

Accept:application/json

Content-Type:application/json; charset=UTF-8

Request Payload: 请求有效负载:

{"jsonrpc":"2.0","method":"changeDetail","params":[{"id":11376}],"id":1}

I already tried this answer but I am getting 400 BAD REQUEST . 我已经尝试过这个答案,但我得到了400 BAD REQUEST

Can anyone help me sort this out? 任何人都可以帮我解决这个问题吗?

Thanks. 谢谢。

The following code works for me. 以下代码适用于我。

//escape the double quotes in json string
String payload="{\"jsonrpc\":\"2.0\",\"method\":\"changeDetail\",\"params\":[{\"id\":11376}],\"id\":2}";
String requestUrl="https://git.eclipse.org/r/gerrit/rpc/ChangeDetailService";
sendPostRequest(requestUrl, payload);

method implementation: 方法实现:

public static String sendPostRequest(String requestUrl, String payload) {
    try {
        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        writer.write(payload);
        writer.close();
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuffer jsonString = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
                jsonString.append(line);
        }
        br.close();
        connection.disconnect();
        return jsonString.toString();
    } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
    }

}

I tried with a rest client. 我尝试了一个休息客户端。

Headers : 标题:

  • POST /r/gerrit/rpc/ChangeDetailService HTTP/1.1 POST / r / gerrit / rpc / ChangeDetailService HTTP / 1.1
  • Host: git.eclipse.org 主持人:git.eclipse.org
  • User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0 User-Agent:Mozilla / 5.0(Windows NT 5.1; rv:18.0)Gecko / 20100101 Firefox / 18.0
  • Accept: application/json 接受:application / json
  • Accept-Language: null Accept-Language:null
  • Accept-Encoding: gzip,deflate,sdch Accept-Encoding:gzip,deflate,sdch
  • accept-charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 accept-charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3
  • Content-Type: application/json; Content-Type:application / json; charset=UTF-8 字符集= UTF-8
  • Content-Length: 73 内容长度:73
  • Connection: keep-alive 连接:保持活力

it works fine. 它工作正常。 I retrieve 200 OK with a good body. 我用一个好身体检索200 OK。

Why do you set a status code in your request? 为什么在请求中设置状态代码? and multiple declaration " Accept " with Accept:application/json,application/json,application/jsonrequest. 使用Accept:application / json,application / json,application / jsonrequest进行多次声明“ Accept ”。 just a statement is enough. 只是一个声明就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 REST:如何发送YAML有效负载(Java) - REST: How to send YAML payload (Java) 如何将带有JSON对象的“放置”请求发送到Java中的JSONPlaceHolder REST API - How to send a “Put” request with a JSON object to a JSONPlaceHolder REST API in Java 如何使用Java Rest客户端在POST负载中传递参数“ request”? - How to pass parameter 'request' in POST payload using Java Rest client? POST请求以JSON对象作为有效负载的REST API - POST request to REST API with JSON object as payload 发送请求有效载荷到Rest API Java修改代码,而无需使用Java中的流读写器 - Sending request payload to rest api java modify the code without stream reader and writer in java Java:发送POST HTTP请求以将文件上传到MediaFire REST API - Java: send POST HTTP request to upload a file to MediaFire REST API 如何使用java在REST api中通过post请求将多个json文档发送到marklogic数据库? - How to send multiple json documents into marklogic database through post request in REST api using java? REST请求有效负载验证 - validation on REST request payload 如何从Java REST服务中的POST请求有效负载中检索值? - How can I retrieve values from POST request payload in Java REST service? 在 REST API 请求有效载荷中提供 POJO object 作为序列化字符串 - Provide a POJO object as serialised string in REST API Request Payload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM