溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

postman模擬post請(qǐng)求的四種請(qǐng)求體分別是什么

發(fā)布時(shí)間:2022-01-07 17:52:18 來(lái)源:億速云 閱讀:360 作者:柒染 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)postman模擬post請(qǐng)求的四種請(qǐng)求體分別是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

1.application/x-www-form-urlencoded

瀏覽器的原生 表單,其中ajax也是用這種方式提交的,主要是key-value 鍵值對(duì)的形式。一般的請(qǐng)求方式如下圖所示:

postman模擬post請(qǐng)求的四種請(qǐng)求體分別是什么

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)請(qǐng)求方式截圖,主要在key中傳入接口中定義的變量,value 中傳入值就可以進(jìn)行測(cè)試接口

2.multipart/form-data

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

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

postman模擬post請(qǐng)求的四種請(qǐng)求體分別是什么

postman模擬post請(qǐng)求的四種請(qǐng)求體分別是什么

PSOT同時(shí)上傳文件和鍵值對(duì)數(shù)據(jù)

3. raw

可以上傳任意格式的文本,可以上傳text、json、xml、html等Controller接口可以通過(guò)@RequestBody 來(lái)修飾,傳入數(shù)據(jù)就是JSON格式

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

postman模擬post請(qǐng)求的四種請(qǐng)求體分別是什么

4.binary

相當(dāng)于Content-Type:application/octet-stream,從字面意思得知,只可以上傳二進(jìn)制數(shù)據(jù),通常用來(lái)上傳文件,由于沒(méi)有鍵值,所以,一次只能上傳一個(gè)文件。

postman模擬post請(qǐng)求的四種請(qǐng)求體分別是什么

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是默認(rèn)的MIME內(nèi)容編碼類型,它在傳輸比較大的二進(jìn)制或者文本數(shù)據(jù)時(shí)效率極低。

MIME:
簡(jiǎn)單說(shuō),MIME類型就是設(shè)定某種擴(kuò)展名的文件用一種應(yīng)用程序來(lái)打開(kāi)的方式類型。服務(wù)器會(huì)將它們發(fā)送的多媒體數(shù)據(jù)的類型告訴瀏覽器,而通知手段就是說(shuō)明該多媒體數(shù)據(jù)的MIME類型,服務(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è)類均實(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;
    }

看完上述內(nèi)容,你們對(duì)postman模擬post請(qǐng)求的四種請(qǐng)求體分別是什么有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI