Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the four request bodies for postman to simulate post requests?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about what the four kinds of request bodies of postman simulation post request are, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

1.application/x-www-form-urlencoded

The native form of the browser, where ajax is also submitted in this way, mainly in the form of key-value key-value pairs. The general request method is shown in the following figure:

POST HTTP/1.1Host: test.app.comContent-Type: application/x-www-form-urlencodedCache-Control: no-cachePostman-Token: e00dbaf5-15e8-3667-6fc5-48ee3cc89758key1=value1&key2=value2

Screenshot of request method in POST (application/x-www-form-urlencoded). The variables defined in the interface are mainly passed in key, and the values are passed in value to test the interface.

2.multipart/form-data

It processes the data of the form as a message, with the label as a unit and separated by a delimiter. You can upload both key-value pairs and files.

Because of boundary isolation, multipart/form-data can upload files as well as key-value pairs. It uses key-value pairs, so you can upload multiple files. In springmvc, you can use MultipartHttpServletRequest to receive different keys according to "name" through api, or you can receive multiple files through the MulTipartFile array.

POST HTTP/1.1Host: test.app.comCache-Control: no-cachePostman-Token: 59227787-c438-361d-fbe1-75feeb78047eContent-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW-WebKitFormBoundary7MA4YWxkTrZu0gWContent-Disposition: form-data; name= "filekey"; filename= "" Content-Type:-WebKitFormBoundary7MA4YWxkTrZu0gWContent-Disposition: form-data; name= "textkey" tttttt-WebKitFormBoundary7MA4YWxkTrZu0gW--

PSOT uploads files and key-value pairs at the same time

3. Raw

You can upload text in any format. You can upload text, json, xml, html and other Controller APIs that can be modified with @ RequestBody. The incoming data is in JSON format.

Note: when using the raw method, if you need to add a key-value (Content-Type: application/json or corresponding format) to the headers when retesting the PostMan

4.binary

Equivalent to Content-Type:application/octet-stream, literally, only binary data can be uploaded, which is usually used to upload files. Since there is no key, only one file can be uploaded at a time.

POST HTTP/1.1Host: test.app.comCache-Control: no-cachePostman-Token: 5ad66f08-6faa-aba0-744a-ca958b1a0fc2undefined

Reminder:

The difference between multipart/form-data and x-www-form-urlencoded:

There are two types of form forms in html: application/x-www-form-urlencoded and multipart/form-data. Application/x-www-form-urlencoded is the default MIME content encoding type, which is extremely inefficient when transferring large binary or text data.

MIME:

Simply put, the MIME type is the way a file with a certain extension is set to be opened by an application. The server will tell the browser the type of multimedia data they send, and the notification means is to indicate the MIME type of the multimedia data, and the server will put the MIME token into the transmitted data to tell the browser which plug-in to use to read the relevant files.

Multipart/form-data: you can upload binary data such as files, or upload form key-value pairs, but eventually turn them into a message. The contentType property is ignored when multipart/form-data,http is set.

X-www-form-urlencoded: you can only upload key-value pairs, not for file upload. Different field are distinguished by &. Both classes implement the HttpEntity interface, using the following:

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 params = new ArrayList (); 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;} after reading the above, do you have any further understanding of what are the four request bodies for postman to simulate post requests? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report