postman模拟post请求的四种请求体
來(lái)源:https://blog.csdn.net/m0_37556444/article/details/82845694
postman的幾種參數(shù)格式
post類(lèi)型的body中可以存放任意的內(nèi)容格式,瀏覽器可以根據(jù)請(qǐng)求頭中指定的content-type類(lèi)型對(duì)請(qǐng)求體進(jìn)行解析。下面介紹postman如何對(duì)四種典型的請(qǐng)求體進(jìn)行模擬。
1. form-data
即multipart/form-data,它將表單的數(shù)據(jù)組織成Key-Value形式,用分隔符boundary(boundary可任意設(shè)置)處理成一條消息。
由于有boundary隔離,所以既可以上傳文件,也可以上傳參數(shù)。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
請(qǐng)求體中的boundary參數(shù)指定的就是分隔體,可以看到請(qǐng)求內(nèi)容被分為了兩段,第一段對(duì)應(yīng)filekey,第二段對(duì)應(yīng)textkey。
2. x-www-form-urlencoded
即application/x-www-from-urlencoded,將表單內(nèi)的數(shù)據(jù)轉(zhuǎn)換為Key-Value。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
3. raw
可以上傳任意格式的【文本】,可以上傳text、json、xml、html等
- 1
- 2
- 3
- 4
- 5
- 6
- 7
4. binary
即Content-Type:application/octet-stream,只可以上傳二進(jìn)制數(shù)據(jù),通常用來(lái)上傳文件。由于沒(méi)有鍵值,所以一次只能上傳一個(gè)文件。
- 1
- 2
- 3
- 4
- 5
- 6
提醒:
multipart/form-data與x-www-form-urlencoded區(qū)別:
html中的form 表單有兩種:application/x-www-form-urlencoded和multipart/form-data。application/x-www-form-urlencoded是默認(rèn)的MIME內(nèi)容編碼類(lèi)型,它在傳輸比較大的二進(jìn)制或者文本數(shù)據(jù)時(shí)效率極低。
MIME:
簡(jiǎn)單說(shuō),MIME類(lèi)型就是設(shè)定某種擴(kuò)展名的文件用一種應(yīng)用程序來(lái)打開(kāi)的方式類(lèi)型。服務(wù)器會(huì)將它們發(fā)送的多媒體數(shù)據(jù)的類(lèi)型告訴瀏覽器,而通知手段就是說(shuō)明該多媒體數(shù)據(jù)的MIME類(lèi)型,服務(wù)器將 MIME標(biāo)志符放入傳送的數(shù)據(jù)中來(lái)告訴瀏覽器使用哪種插件讀取相關(guān)文件。
multipart/form-data:既可以上傳文件等二進(jìn)制數(shù)據(jù),也可以上傳表單鍵值對(duì),只是最后會(huì)轉(zhuǎn)化為一條信息。當(dāng)設(shè)置multipart/form-data,http會(huì)忽略 contentType 屬性。
x-www-form-urlencoded:只能上傳鍵值對(duì),不能用于文件上傳。不同的field是用&區(qū)分開(kāi)的。這兩個(gè)類(lèi)均實(shí)現(xiàn)了HttpEntity接口,使用如下:
public static String testUpload(String url) {String result = null;CloseableHttpClient httpclient = HttpClients.createDefault();HttpPost httppost = new HttpPost(url);try {FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg"));StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build();httppost.setEntity(reqEntity);System.out.println("executing request " + httppost.getRequestLine());CloseableHttpResponse response = httpclient.execute(httppost);try {int statusCode = response.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_OK) {result = EntityUtils.toString(response.getEntity(), "UTF-8");}} finally {response.close();httpclient.close();}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}return result;}public static String testParam(String url) {String result = null;CloseableHttpClient httpclient = HttpClients.createDefault();httpclient = HttpsHelper.newHttpsCloseableClient();HttpPost httpPost = new HttpPost(url);List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("key1", "value1"));params.add(new BasicNameValuePair("key2", "value2"));try {httpPost.setEntity(new UrlEncodedFormEntity(params));httpPost.setConfig(requestConfig);CloseableHttpResponse httpResp = httpclient.execute(httpPost);try {int statusCode = httpResp.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_OK) {result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");}} finally {httpResp.close();httpclient.close();}} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}return result;}總結(jié)
以上是生活随笔為你收集整理的postman模拟post请求的四种请求体的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ThinkPHP5跨控制器调用
- 下一篇: ajax 设置Access-Contro