java 如何在restTemplate中以application/x-www-form-urlencoded的形式发送body

wqnecbli  于 2023-08-02  发布在  Java
关注(0)|答案(1)|浏览(212)

可以在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(MediaType.APPLICATION_JSON, APPLICATION_URLENCODED.getValue());
        HttpEntity<?> requestEntity = new HttpEntity<>(gson.toJson(requestBody), headers);


Postman 截图:x1c 0d1x的数据

yhxst69z

yhxst69z1#

最后,我发现在“application/x-www-form-urlencoded”中,我们必须使用以下内容:

MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("username",propertyConfig.getUserName());

  HttpHeaders headers = new HttpHeaders();
    headers.add(MediaType.APPLICATION_JSON, "application/x-www-form-urlencoded");
    HttpEntity<?> requestEntity = new HttpEntity<>(requestBody, headers);

字符串

相关问题