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隔離,所以既可以上傳文件,也可以上傳參數。
- 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。
- 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,只可以上傳二進制數據,通常用來上傳文件。由于沒有鍵值,所以一次只能上傳一個文件。
- 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请求的四种请求体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ThinkPHP5跨控制器调用
- 下一篇: 上班兼职两不误的副业 愿意做就能赚钱