简体   繁体   English

如何使用 java restTeplate 将原始数据添加到正文请求

[英]how to add raw data to body request using java restTeplate

How to add raw data like below sample as body request using java rest-template如何使用 java rest-template 添加如下示例的原始数据作为正文请求

{
  "body": {
    "content": "This is sent via postman to MS-team general channel"
  }
}

i was having difficulty in sending raw data in request body using java rest-template, so i'm adding this code here for future reference(i'm a newbie to java coding).我在使用 java 其余模板在请求正文中发送原始数据时遇到困难,所以我在此处添加此代码以供将来参考(我是 java 编码的新手)。

Below code worked for me for sending a message to MS-Team channel下面的代码对我有用,可以向 MS-Team 频道发送消息

    public JSONObject sendMessage(String team_ID, String channel_ID){
        JSONObject res =null;        
        RestTemplate restTemplate = new RestTemplate();
        String url="https://graph.microsoft.com/beta/teams/"+team_ID+"/channels/"+channel_ID+"/messages";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);                
        String token = "xxxxxxxxxx";
        headers.set("Authorization","Bearer "+token);
        


         // This  didn't worked 
        /*MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
        map.add("content", "Welcome to General channel message sent via intellij IDE");

        MultiValueMap<String, String> map1= new LinkedMultiValueMap<String, String>();
        map1.add("content", "Welcome to General channel message sent via intellij IDE");
        //map.add("body", "Welcome to General channel message sent via intellij IDE");*/
        
         // This one also didn't worked 
        //JSONObject jsonObject = new JSONObject();
        //jsonObject.put("body", "{ \"body\": {\"content\":\"Welcome to General channel message sent via intellij IDE  using java coding & http rest-Template\"}}");

        // This one also didn't worked 
        //JSONObject jsonObject = new JSONObject();
        //jsonObject.put("body", "{\"content\":\"Welcome to General channel message sent via intellij IDE  using java coding & http rest-Template\"}");
        
        // This one worked
        String requestJson = "{ \"body\": {\"content\":\"Welcome to General channel message sent via intellij IDE using java coding & http rest-Template\"}}";
        
                 
        // HttpEntity<String> request = new HttpEntity<String>(jsonObject.toString(),headers);
        HttpEntity<String> request = new HttpEntity<String>(requestJson,headers);
        ResponseEntity<String> response=null;
        try {
            response = restTemplate.postForEntity(url, request, String.class);
            res= new JSONObject(response.getBody());
            System.out.println(res);
        }catch(Exception e){
            e.printStackTrace();
        }
        return  res;
    }

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

相关问题 如何在非GUI模式下使用Java代码进行JMeter测试的post请求中添加请求体数据? - How to add the request body data in the post request for JMeter testing using Java code in the non-GUI mode? 在请求正文中发送原始数据 - Sending raw data in request body I want to pass json object as a raw data using @Body in retrofit, but I am unable to add an image file inside the Request Object, how can I achieve it - I want to pass json object as a raw data using @Body in retrofit, but I am unable to add an image file inside the Request Object, how can I achieve it 如何使用Java进行POST并包含参数和原始请求体? - How can I POST using Java and include parameters and a raw request body? 如何使用Java在jmeter中获取身体数据请求 - How to get body data request in jmeter using java 如何从Postman到Java获取Body原始JSON数据 - how to get Body raw JSON data from Postman to Java 访问http请求正文原始数据 - Access to http request body raw data 如何使用Java读取原始串行数据? - How to read raw serial data using Java? 使用改造的邮政请求正文中的原始字符串 - raw string in post request body using retrofit 如何使用Java Spring的RestTemplate的exchange方法将主体添加到HTTP请求? - How to add a body to a HTTP request using Java Spring's RestTemplate's exchange method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM