繁体   English   中英

如何以 application/x-www-form-urlencoded 在 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);

Postman 截图: 在此处输入图像描述

最后我发现在“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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM