postman模擬post請求的四種請求體
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
它會將表單的數(shù)據(jù)處理為一條消息,以標簽為單元,用分隔符分開。既可以上傳鍵值對,也可以上傳文件。
由于有boundary隔離,所以multipart/form-data既可以上傳文件,也可以上傳鍵值對,它采用了鍵值對的方式,所以可以上傳多個文件,在springmvc中可以使用MultipartHttpServletRequest接收通過api根據(jù)"name"獲取不同的鍵值,也可以通過MulTipartFile數(shù)組接收多個文件。
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同時上傳文件和鍵值對數(shù)據(jù)
3. raw
可以上傳任意格式的文本,可以上傳text、json、xml、html等Controller接口可以通過@RequestBody 來修飾,傳入數(shù)據(jù)就是JSON格式
注意: 在使用raw 方式,如果在PostMan再測試的時候需要在headers中添加一個key-value (Content-Type: application/json 或者對應的格式)
4.binary
相當于Content-Type:application/octet-stream,從字面意思得知,只可以上傳二進制數(shù)據(jù),通常用來上傳文件,由于沒有鍵值,所以,一次只能上傳一個文件。
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區(qū)別:
html中的form 表單有兩種:application/x-www-form-urlencoded和multipart/form-data。application/x-www-form-urlencoded是默認的MIME內(nèi)容編碼類型,它在傳輸比較大的二進制或者文本數(shù)據(jù)時效率極低。
MIME:
簡單說,MIME類型就是設定某種擴展名的文件用一種應用程序來打開的方式類型。服務器會將它們發(fā)送的多媒體數(shù)據(jù)的類型告訴瀏覽器,而通知手段就是說明該多媒體數(shù)據(jù)的MIME類型,服務器將 MIME標志符放入傳送的數(shù)據(jù)中來告訴瀏覽器使用哪種插件讀取相關文件。
multipart/form-data:既可以上傳文件等二進制數(shù)據(jù),也可以上傳表單鍵值對,只是最后會轉化為一條信息。當設置multipart/form-data,http會忽略 contentType 屬性。
x-www-form-urlencoded:只能上傳鍵值對,不能用于文件上傳。不同的field是用&區(qū)分開的。這兩個類均實現(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; ? ? }
到此這篇關于postman模擬post請求的四種請求體的文章就介紹到這了,更多相關postman post請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vscode配置leetcode插件并解決無法登錄問題(圖文詳解)
這篇文章主要介紹了vscode配置leetcode插件并解決無法登錄問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06JetBrains公司三大編輯器迭代循環(huán)模板快捷鍵詳解
這篇文章主要介紹了JetBrains公司三大編輯器迭代循環(huán)模板快捷鍵,如果快捷鍵無用,請到keymap中調(diào)整自己的快捷鍵,或者查看是否有應用占用了該快捷鍵,需要的朋友可以參考下2022-04-04