![](/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.