[英]How to add the request body data in the post request for JMeter testing using Java code in the non-GUI mode?
[英]how to add raw data to body request using java restTeplate
如何使用 java rest-template 添加如下示例的原始數據作為正文請求
{
"body": {
"content": "This is sent via postman to MS-team general channel"
}
}
我在使用 java 其余模板在請求正文中發送原始數據時遇到困難,所以我在此處添加此代碼以供將來參考(我是 java 編碼的新手)。
下面的代碼對我有用,可以向 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.