首页IT科技httpclient设置请求头(配置请求头Content-Type)

httpclient设置请求头(配置请求头Content-Type)

时间2025-08-02 16:20:40分类IT科技浏览6398
导读:Content-Type有三种类型 // 1 默认的格式请求体中的数据会以json字符串的形式发送到后端...

Content-Type有三种类型

// 1 默认的格式请求体中的数据会以json字符串的形式发送到后端 Content-Type: application/json // 2 请求体中的数据会以普通表单形式(键值对)发送到后端 Content-Type: application/x-www-form-urlencoded // 3 它会将请求体的数据处理为一条消息                ,以标签为单元                        ,用分隔符分开                。既可以上传键值对        ,也可以上传文件 Content-Type: multipart/form-data //4.纯文体的传输                        。空格转换为 “+                ” 加号            ,但不对特殊字符编码        。 ‘Content-Type:text/plain

我们可以这样配置请求头

axios({ headers: { Content-Type: application/x-www-form-urlencoded }, method: post, url: url, data: Qs.stringify(params) })

或者是写到post的js文件

axios.js

axios.defaults.headers.post[Content-Type] = "application/x-www-form-urlencoded"

对于不同的请求头                        ,后端接口要用不同的注解吸收数据            ,否则会报404

multipart/form-data指定传输数据为二进制类型        ,比如图片                、mp3                        、文件            。

text/plain:纯文体的传输                        。空格转换为 “+                        ” 加号                        ,但不对特殊字符编码            。

@RequestParam

1.均支持POST,GET请求

2.只支持Content-Type: 为 application/x-www-form-urlencoded编码的内容        。Http协议中                ,如果不指定Content-Type    ,则默认传递的参数就是application/x-www-form-urlencoded类型) @PostMapping public Result shopBtn(@RequestParam String token){ System.out.println(token); return userShopService.checkShop(token); }

@RequestBody绑定一个对象实体

1.不支持get请求                        ,因为get请求没有HttpEntity

2.必须要在请求头中申明content-Type(如application/json).springMvc通过HandlerAdapter配置的HttpMessageConverters解析httpEntity的数据                    ,并绑定到相应的bean上

3.只能一个@RequestBody                        。

4.可以与@RequestParam一起使用,但建议最好不要与@RequestParam一起使用                    ,是因为@RequestBody会将InputStream吃掉                        ,造成后面的@RequsetParam无法匹配到参数而报400

  @PostMapping("/shopLogin") public Result shopLogin(@RequestBody User user){ return userService.shopLogin(user); }

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
佳能2900打印机卡纸问题维修视频(佳能2900打印机卡纸问题维修) vue3路由跳转传对象(vue-router路由跳转与打开新窗口)