日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

postman模拟post请求的四种请求体

發布時間:2024/9/20 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 postman模拟post请求的四种请求体 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

來源:https://blog.csdn.net/m0_37556444/article/details/82845694

postman的幾種參數格式

post類型的body中可以存放任意的內容格式,瀏覽器可以根據請求頭中指定的content-type類型對請求體進行解析。下面介紹postman如何對四種典型的請求體進行模擬。

1. form-data
即multipart/form-data,它將表單的數據組織成Key-Value形式,用分隔符boundary(boundary可任意設置)處理成一條消息。
由于有boundary隔離,所以既可以上傳文件,也可以上傳參數。

POST HTTP/1.1 Host: test.app.com Cache-Control: no-cache Postman-Token: 59227787-c438-361d-fbe1-75feeb78047e Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="filekey"; filename="" Content-Type: ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="textkey"tttttt ------WebKitFormBoundary7MA4YWxkTrZu0gW--
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

請求體中的boundary參數指定的就是分隔體,可以看到請求內容被分為了兩段,第一段對應filekey,第二段對應textkey。

2. x-www-form-urlencoded
即application/x-www-from-urlencoded,將表單內的數據轉換為Key-Value。

POST HTTP/1.1 Host: test.app.com Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache Postman-Token: e00dbaf5-15e8-3667-6fc5-48ee3cc89758key1=value1&key2=value2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3. raw
可以上傳任意格式的【文本】,可以上傳text、json、xml、html等

POST HTTP/1.1 Host: test.app.com Content-Type: application/json Cache-Control: no-cache Postman-Token: 05a064d2-fa79-10c0-caba-15ca5d1a940f{"key1":"value1","key2":"value2"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4. binary
即Content-Type:application/octet-stream,只可以上傳二進制數據,通常用來上傳文件。由于沒有鍵值,所以一次只能上傳一個文件。

POST HTTP/1.1 Host: test.app.com Cache-Control: no-cache Postman-Token: 5ad66f08-6faa-aba0-744a-ca958b1a0fc2undefined
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

提醒:
multipart/form-data與x-www-form-urlencoded區別:
  html中的form 表單有兩種:application/x-www-form-urlencoded和multipart/form-data。application/x-www-form-urlencoded是默認的MIME內容編碼類型,它在傳輸比較大的二進制或者文本數據時效率極低。

MIME:
簡單說,MIME類型就是設定某種擴展名的文件用一種應用程序來打開的方式類型。服務器會將它們發送的多媒體數據的類型告訴瀏覽器,而通知手段就是說明該多媒體數據的MIME類型,服務器將 MIME標志符放入傳送的數據中來告訴瀏覽器使用哪種插件讀取相關文件。

multipart/form-data:既可以上傳文件等二進制數據,也可以上傳表單鍵值對,只是最后會轉化為一條信息。當設置multipart/form-data,http會忽略 contentType 屬性。

x-www-form-urlencoded:只能上傳鍵值對,不能用于文件上傳。不同的field是用&區分開的。這兩個類均實現了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;}

總結

以上是生活随笔為你收集整理的postman模拟post请求的四种请求体的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。