日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

postman模擬post請求的四種請求體_相關技巧

作者:孤獨的行走 ? 更新時間: 2022-03-26 編程語言

1.application/x-www-form-urlencoded

瀏覽器的原生 表單,其中ajax也是用這種方式提交的,主要是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-48ee3cc89758

key1=value1&key2=value2

POST中(application/x-www-form-urlencoded)請求方式截圖,主要在key中傳入接口中定義的變量,value 中傳入值就可以進行測試接口

2.multipart/form-data

它會將表單的數據處理為一條消息,以標簽為單元,用分隔符分開。既可以上傳鍵值對,也可以上傳文件。
由于有boundary隔離,所以multipart/form-data既可以上傳文件,也可以上傳鍵值對,它采用了鍵值對的方式,所以可以上傳多個文件,在springmvc中可以使用MultipartHttpServletRequest接收通過api根據"name"獲取不同的鍵值,也可以通過MulTipartFile數組接收多個文件
。

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--

在這里插入圖片描述

在這里插入圖片描述

PSOT同時上傳文件和鍵值對數據

3. raw

可以上傳任意格式的文本,可以上傳text、json、xml、html等Controller接口可以通過@RequestBody 來修飾,傳入數據就是JSON格式

注意: 在使用raw 方式,如果在PostMan再測試的時候需要在headers中添加一個key-value (Content-Type: application/json 或者對應的格式)

在這里插入圖片描述

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-ca958b1a0fc2

undefined

?提醒:
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;
? ? }

原文鏈接:https://blog.csdn.net/lq18894033018/article/details/104703978

欄目分類
最近更新