简体   繁体   English

如何以 application/x-www-form-urlencoded 在 restTemplate 中发送正文

[英]How to send body in restTemplate as application/x-www-form-urlencoded

It is possible to set "application/x-www-form-urlencoded" in HttpHeader, but I want to set for requestbody, could you please guide me?可以在 HttpHeader 中设置“application/x-www-form-urlencoded”,但我想为 requestbody 设置,请您指导我吗?

sample json:样品 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"
                        }
                    ]
                },

code:代码:

        HttpHeaders headers = new HttpHeaders();
        headers.add(PostEnum.CONTENT_TYPE.getValue(), PostEnum.APPLICATION_URLENCODED.getValue());
        HttpEntity<?> requestEntity = new HttpEntity<>(gson.toJson(requestBody), headers);

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

Finally I found out that in "application/x-www-form-urlencoded" we have to use as following:最后我发现在“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.

相关问题 如何使用Android批注和RestTemplate在POST请求的正文中发送x-www-form-urlencoded - How to send x-www-form-urlencoded in a body of POST request using android annotations and resttemplate 如何使用 x-www-form-urlencoded 正文发送 post 请求 - How to send post request with x-www-form-urlencoded body 未找到Form&application / x-www-form-urlencoded的Body Writer - Body Writer not found for Form & application/x-www-form-urlencoded Spring RestTemplate如何在application / x-www-form-urlencoded中使用POJO? - Spring RestTemplate how to use POJO with application/x-www-form-urlencoded? FeignClient 使用 application/x-www-form-urlencoded 正文创建 POST - FeignClient create POST with application/x-www-form-urlencoded body 使用 ClientBuilder 发送 JSON 正文,但使用 ContentType=application/x-www-form-urlencoded - Send JSON body but with ContentType=application/x-www-form-urlencoded with ClientBuilder 如何使用 webclient 发布正文 x-www-form-urlencoded? - how to post body x-www-form-urlencoded using webclient? 如何在 HTTPURLConnection 中以 application/x-www-form-urlencoded 格式向 POST 调用添加正文 - How to add a body to POST call in HTTPURLConnection in application/x-www-form-urlencoded format 如何使用关联数组解析 application/x-www-form-urlencoded? - How to parse a application/x-www-form-urlencoded with an associative array? 使用 RestTemplate 与对象作为数据和 application/x-www-form-urlencoded 内容类型? - Use RestTemplate with object as data and application/x-www-form-urlencoded content type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM