post请求 form-data类型请求数据 --springboot restTemplate
Java——RestTemplate发送POST请求之formData形式

import com.alibaba.fastjson.JSON; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.util.*; /** * RestTemplate发送POST请求之formData形式 * @return */ public static Map<String, String> formData(){ String url = "http://192.168.1.85:8089/getToken"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); //接口参数 map.add("username","xiaoming"); map.add("password","123456"); //头部类型 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); //构造实体对象 HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers); //发起请求,服务地址,请求参数,返回消息体的数据类型 ResponseEntity<String> response = restTemplate.postForEntity(url, param, String.class); //body String body = response.getBody(); System.out.println(body); //JSON格式转为Map类型 Map result = JSON.parseObject(body, Map.class); System.out.println(result); return result; }
参考:
HttpURLConnection post请求“Content-Type“, “multipart/form-data“方式进行请求操作
JAVA HttpURLConnection 发送post请求,数据格式为form-data,form-data传文件,传日志
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升