postman请求参数的格式

postman中 http请求参数常用的格式有三种

1. form-data

2. x-www-form-urlencoded

3. row

1. form-data

使用form-data格式时, Content-Type 为

multipart/form-data; boundary=------WebKitFormBoundaryxapCX9v3I390PUpX

bounary的值表示分隔符

报文格式为:

------WebKitFormBoundaryxapCX9v3I390PUpX
Content-Disposition: form-data; name="name"

johndoe
------WebKitFormBoundaryxapCX9v3I390PUpX
Content-Disposition: form-data; name="id"

123456
------WebKitFormBoundaryxapCX9v3I390PUpX--

boundary 后面定义的分隔符和请求报文中的分隔符一样,后端就可以将参数解析出来

这种方式可以用于 普通参数和文件 的上传

var formData = new FormData();
        formData.append(name, johndoe);
        formData.append(id, 123456);
        var xhr = new XMLHttpRequest();
        xhr.timeout = 30000;
        xhr.responseType = "json";
        xhr.open(POST, http://localhost:8090/springProject/testController1/upLoad1, true);
        xhr.send(formData);

编写ajax请求的时候,不要自己指定Content-Type,自己指定的bounary浏览器生成的bounary不一样,将导致无法解析

2. x-www-form-urlencoded

这个望文生义,也是表单,只是把表单的参数做了url编码

报文格式为 name=fengxing&sex=male

需要指定Content-Type, 这种方式显然是不太方便传文件的

var formData = new FormData();
        formData.append(name, johndoe);
        formData.append(id, 123456);
        var xhr = new XMLHttpRequest();
        xhr.timeout = 30000;
        xhr.responseType = "json";
        xhr.open(POST, http://localhost:8090/springProject/testController1/upLoad2, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send("name=johndoe&sex=male");

3. row

row可以定义下面这些格式,开发中常用的是json,估计自己定义格式也是可以的,本质上只要保证传参和取参的规则一致就可以了

经验分享 程序员 微信小程序 职场和发展