欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

x-www-form-urlencoded到底是什么? 有大用

概述:

x-www-form-urlencoded纸面翻译即所谓url格式的编码,是post的默认Content-Type,其实我觉得可以认为get和post的默认表单数据传递格式都一样,只是一个在url地址后面加 ?再加表单数据,另一个是把表单数据写在请求体內

一、位置:

  • 请求头內的Content-Type字段里,

二、写法:

Content-Type:application/x-www-form-urlencoded            

三、用处:

  1. get请求的请求体格式是什么?get请求是拼接在url后面请求的,一般如此username=tom&pwd=123,这样的格式叫查询参数,x-www-form-urlencoded也长这样,只是不添加到url后面;

  2. 要知道post的默认数据传输格式就是x-www-form-urlencoded,所以为什么在post数据的时候需要把数据转为url格式(username=tom&pwd=123),一般使用qs库的qs.stringify()方法就能把json对象转换成url格式编码(x-www-form-urlencoded


           

来自  https://blog.csdn.net/qq_29923881/article/details/103500948            


           


       

关于接收x-www-form-urlencoded格式数据的后端

   




前言

通常在Springboot接收参数时候,都是json格式的传输,使用restful风格,而在实际开发中,可能会接收其它格式的参数,比如x-www-form-urlencoded格式的数据

一、x-www-form-urlencoded格式是什么?

就是application/x-www-from-urlencoded,会将表单内的数据转换为键值对,比如,name=zhangsan&age = 18

二、后端如何接收

1.后端代码

1.(使用@RequestBody+ String类型接收 )

代码如下(示例):

    @PostMapping("/efly/get")
    @ResponseBody
    public RespEntity getEFlyReturnPR(@RequestBody String cd) {
        System.out.println("接收到的x-www-form-urlencoded");
        System.out.println(cd);
        return respEntity=new RespEntity(RespCode.CODE_200,cd);
    }


2.(使用@RequestBody+ Map类型接收 )

代码如下(示例):

    @PostMapping("/efly/get")
    @ResponseBody
    public RespEntity getEFlyReturnPR(@RequestParam Map<String, String> params) {
        System.out.println("接收到的x-www-form-urlencoded");
        System.out.println(params);
        return respEntity=new RespEntity(RespCode.CODE_200,params);
    }
}


3.(使用@RequestBody+ LinkedHashMap类型接收 )

代码如下(示例):

    @PostMapping("/efly/get")
    @ResponseBody
    public RespEntity getEFlyReturnPR(@RequestParam LinkedHashMap<String, String> params) {
        System.out.println("接收到的x-www-form-urlencoded");
        System.out.println(params);
        return respEntity=new RespEntity(RespCode.CODE_200,params);
    }
}


postman:
在这里插入图片描述
控制台输出:
在这里插入图片描述

使用map数据类型:
在这里插入图片描述
在这里插入图片描述

2.(错误的)后端代码(使用@RequestBody+ 自己封装类型接收 )

postman:
在这里插入图片描述
自己封装的实体:
在这里插入图片描述
后端代码:
在这里插入图片描述
postman报错:
在这里插入图片描述

3.(错误的)后端代码( 单独String类型接收 )

在这里插入图片描述

总结

当传入参数是x-www-form-urlencoded,接收参数@RequestBody+ String/Map/LinkedHashMap即可接收成功

    @PostMapping("/efly/get")
    @ResponseBody
    public RespEntity getEFlyReturnPR(@RequestBody String cd) {
        System.out.println("接收到的x-www-form-urlencoded");
        System.out.println(cd);
        return respEntity=new RespEntity(RespCode.CODE_200,cd);
    }
}


    @PostMapping("/efly/get")
    @ResponseBody
    public RespEntity getEFlyReturnPR(@RequestBody Map<String, String> params) {
        System.out.println("接收到的x-www-form-urlencoded");
        System.out.println(params);
        return respEntity=new RespEntity(RespCode.CODE_200,cd);
    }


    @PostMapping("/efly/get")
    @ResponseBody
    public RespEntity getEFlyReturnPR(@RequestParam LinkedHashMap<String, String> params) {
        System.out.println("接收到的x-www-form-urlencoded");
        System.out.println(params);
        return respEntity=new RespEntity(RespCode.CODE_200,params);
    }
}
   
   来自  https://blog.csdn.net/weixin_51614089/article/details/117067441  
   
               
普通分类: