java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot发送post请求

Springboot发送post请求的几种方式总结

作者:qq_42607375

这篇文章主要为大家详细介绍了Springboot发送post请求的几种方式,文中的示例代码讲解详细,对我们学习或工作有一定的帮助,感兴趣的小伙伴可以了解一下

请求体为json形式

/***
url:请求路径
content: 请求参数,为json字符串
token: 请求认证token,放置在请求头里
*/
public ResponseEntity<String> callApi(String url,  String content, String token) {
        RestTemplate restTemplate = new RestTemplate();
        // 设置请求头,指定Content-Type为application/json
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 添加Authorization头
        headers.add(Constants.AUTHORIZATION, Constants.TOKEN_PREFIX + shareToken);
        // 创建HttpEntity,将JSON参数和请求头添加到请求中
        HttpEntity<String> requestEntity = new HttpEntity<>(content, headers);
        // 发送POST请求
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return responseEntity;
    }

请求体为x-www-form-urlencoded

public ResponseEntity<String> callApi(String url,  String content, String token) {
        RestTemplate restTemplate = new RestTemplate();
        // 设置请求头,指定Content-Type为application/json
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        // 添加Authorization头
        headers.add(Constants.AUTHORIZATION, Constants.TOKEN_PREFIX + shareToken);
        JSONObject jsonObject = JSONObject.parseObject(content);
         // 将解析后的 JSON 对象转换为 MultiValueMap
         MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
         for (String key : jsonObject.keySet()) {
             formData.add(key, jsonObject.get(key));
         }
         HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(formData, headers);
         responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return responseEntity;
    }

请求体为form-data

public ResponseEntity<String> callApi(String url,  String content, String token) {
        RestTemplate restTemplate = new RestTemplate();
        // 设置请求头,指定Content-Type为application/json
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA_VALUE);
        // 添加Authorization头
        headers.add(Constants.AUTHORIZATION, Constants.TOKEN_PREFIX + shareToken);
        JSONObject jsonObject = JSONObject.parseObject(content);
            // 将解析后的 JSON 对象转换为 MultiValueMap
            MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
            for (String key : jsonObject.keySet()) {
                formData.add(key, jsonObject.get(key));
            }
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(formData, headers);
            responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return responseEntity;
    }

到此这篇关于Springboot发送post请求的几种方式总结的文章就介绍到这了,更多相关Springboot发送post请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文