日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/9/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 postman模拟post请求的四种请求体 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

來(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ù)。

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

請(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。

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,只可以上傳二進(jìn)制數(shù)據(jù),通常用來(lái)上傳文件。由于沒(méi)有鍵值,所以一次只能上傳一個(gè)文件。

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區(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。