![](/img/trans.png)
[英]How to send x-www-form-urlencoded in a body of POST request using android annotations and resttemplate
[英]How to send body in restTemplate as application/x-www-form-urlencoded
可以在 HttpHeader 中設置“application/x-www-form-urlencoded”,但我想為 requestbody 設置,請您指導我嗎?
樣品 json:
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "username",
"value": "Tohid",
"type": "text"
},
{
"key": "password",
"value": "*mk",
"type": "text"
},
{
"key": "grant_type",
"value": "password",
"type": "text"
}
]
},
代碼:
HttpHeaders headers = new HttpHeaders();
headers.add(PostEnum.CONTENT_TYPE.getValue(), PostEnum.APPLICATION_URLENCODED.getValue());
HttpEntity<?> requestEntity = new HttpEntity<>(gson.toJson(requestBody), headers);
最后我發現在“application/x-www-form-urlencoded”中我們必須使用如下:
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
requestBody.add("username",propertyConfig.getUserName());
HttpHeaders headers = new HttpHeaders();
headers.add(PostEnum.CONTENT_TYPE.getValue(), PostEnum.APPLICATION_URLENCODED.getValue());
HttpEntity<?> requestEntity = new HttpEntity<>(requestBody, headers);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.