gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:22:02 31 4
gpt4 key购买 nike

可以在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 截图: enter image description here

最佳答案

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

关于java - 如何在 restTemplate 中将正文发送为 application/x-www-form-urlencoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66277274/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com