龙空技术网

http请求的content-type及使用场景

风火轮大师兄kk 127

前言:

此刻我们对“请求头设置编码格式”大概比较着重,姐妹们都需要学习一些“请求头设置编码格式”的相关资讯。那么小编在网上收集了一些有关“请求头设置编码格式””的相关文章,希望姐妹们能喜欢,朋友们一起来了解一下吧!


常见Content-type类型

在HTTP协议消息头中,使用Content-Type来表示媒体类型信息。它被用来告诉服务端如何处理请求的数据,以及告诉客户端(一般是浏览器)如何解析响应的数据,比如显示图片,解析html或仅仅展示一个文本等。

Post请求的内容放置在请求体中,Content-Type定义了请求体的编码格式。数据发送出去后,还需要接收端解析才可以。接收端依靠请求头中的Content-Type字段来获知请求体的编码格式,最后再进行解析。开发过程中主要有如下几种content-type类型:

text/xml
该种方式主要用来提交XML格式的数据。

application/x-www-form-urlencoded
浏览器的原生form表单,如果不设置enctype属性,那么最终会以applicatiion/x-www-form-urlencoded方式提交数据。这种方式提交数据放在body里面,数据按照key1=value1&key2=value2的方式进行编码。

multipart/form-data
这种方式也是常见的post提交方式,通常表单上传时使用该方法。

application/json
告诉服务器主体的序列化的json字符串。

开发过程中主要用到“application/x-www-form-urlencoded”、“application/json”、“multipart/form-data”三种类型,下面我们就来详细说说这三种类型的结构和在SpringMVC中的使用场景:

当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url。 当action为post时候,浏览器把form数据封装到http body中,然后发送到server
客户端:
header:Content-Type=application/x-www-form-urlencoded
Method:get方法-参数需要做URLEncode
Post方法-构建nameValuePairList列表放入UrlEncodedFormEntity

List<NameValuePair> nameValuePairList = new ArrayList<~>();        nameValuePairList.add(new BasicNameValuePair( name: "name", value: "zhangsan"));        nameValuePairList.add(new BasicNamevaluePair( name: "birthday" value: "1990-08-25"));        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(namevaluePairlist, charset: "UTF-8");        post.setEntity(entityParam);

服务端:
请求参数不含MultlpartFile类型时可同时支持 GET和POST,具体请参考以下规范:
controller方法注解@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET})
上传文件:只支持POST(包括MutipleFile和Base64字符串)
方法参数可以对象构成:不能加@RequestBody注解,否则不能接收到

    @RequestMapping(value = "/testParamForm", method = {RequestMethod.POST,RequestMethod.GET})    @ApiOperation("Content-type:application/x-www-form-urlencoded, 同时支持POST和GET,多个请求参数,无上传文件, URL和body的请求参数可以正常获取,URL的参数encode转码 ")    @ApiResponses(value={@ApiResponse(code = 200, message = "请求成功")})    public Result testParamForm(String name, String idcard){        log.info("name:{}, idcard:{}", name, idcard);        return ResultUtil.success();    }

如果是请求参数超过3个以上,可以封装成请求参数对象:

    @RequestMapping(value = "/testObjForm", method = {RequestMethod.POST,RequestMethod.GET})    @ApiOperation("Content-type:application/x-www-form-urlencoded, 同时支持POST和GET,封装请求参数对象,无上传文件 ")    @ApiResponses(value={@ApiResponse(code = 200, message = "请求成功")})    public Result testObjForm(IdcardRequestDto idcardRequestDto){        log.info("name:{}, idcard:{}, file: {}", idcardRequestDto.getName(), idcardRequestDto.getIdcard(), idcardRequestDto.getFile());        return ResultUtil.success();    }

客户端:
header:Content-Type=application/json
Mehthod:POST
请求参数json字符串,格式如:“{“name”:“张三”}” ,可通过构建map对象后再转换成json字符串:JSONObject.toJSONString(map)
服务端:
Method只支持POST @RequestMapping(method = {RequestMethod.POST})
方法参数可以对象构成:加@RequestBody 注解前缀,否则不能接收到
文件上传可以通过转换成base64参数

    @RequestMapping(value = "/testJson", method = {RequestMethod.POST})    @ApiOperation("Content-type:application/json, 只支持POST,请求参数可以是对象,List,Map,如有上传文件,需转换成Base64字符串 ")    @ApiResponses(value={@ApiResponse(code = 200, message = "请求成功")})    public Result testJson(@RequestBody IdcardRequestDto idcardRequestDto){        log.info("name:{}, idcard:{}, file: {}", idcardRequestDto.getName(), idcardRequestDto.getIdcard(), idcardRequestDto.getFile());        return ResultUtil.success();    }

当请求参数有上传文件,3个及以下请求参数,只支持POST请求,客户端设置请求头参数:“Content-type: multipart/form-data”:

    @RequestMapping(value = "/testMutipartParamForm", method = {RequestMethod.POST})    @ApiOperation("Content-type:application/x-www-form-urlencoded, 只支持POST,多个请求参数,有上传文件 ")    @ApiResponses(value={@ApiResponse(code = 200, message = "请求成功")})    public Result testMutipartParamForm(String name, String idcard, MultipartFile file){        log.info("name:{}, idcard:{}, file: {}", name, idcard, file);        return ResultUtil.success();    }

当请求参数有上传文件,3个以上请求参数,封装成请求对象,不能加@RequestBody注解,只支持POST请求,客户端设置请求头参数:“Content-type: multipart/form-data”

    @RequestMapping(value = "/testMutipartObjForm", method = {RequestMethod.POST})    @ApiOperation("Content-type:application/x-www-form-urlencoded, 只支持POST,封装请求参数对象,有上传文件 ")    @ApiResponses(value={@ApiResponse(code = 200, message = "请求成功")})    public Result testMutipartObjForm(IdcardRequestDto idcardRequestDto){        log.info("name:{}, idcard:{}, file: {}", idcardRequestDto.getName(), idcardRequestDto.getIdcard(), idcardRequestDto.getFile());        return ResultUtil.success();    }

本案例来自于昂焱数据,更多精彩请访问


标签: #请求头设置编码格式