The difference between Request Payload, Query String Parameters, and Form Data for HTTP requests

When transferring data with the server, the GET and POST methods are usually used for parameter submission, and the method of parameter submission usually depends on the way the server receives the data.

1.Request Payload

When a POST request is initiated, if the content-type is application/json, the parameters will be passed in the form of Request Payload (obviously, the data format is JSON), and will not appear explicitly in the request url.
headers:
Insert picture description hereincoming parameters:
Insert picture description here

common problem:

The vue axois request interface is parsed as Request Payload by default.

If you want to pass data through Form Data, you can use the native method formData() to assemble data, and the content-type needs to be set to multipart/form-data.

Liberation plan:
Change'Content-Type':'applicacaton/json' to:'Content-Type':'application/x-www-form-urlencoded', the
method is as follows:

// 通用公用方法
const req = (method, url, params) => {  
  return axios({   
     method: method,  
     url: url,      
    headers: { 
    'Content-Type': 'application/x-www-form-urlencoded',     
  },       
   data: params,     
  traditional: true,    
 transformRequest: [  
 function(data) {     
       let ret = ''              
       for (let it in data) {     
         ret +=                    
        encodeURIComponent(it) +            
          '=' +                      
         encodeURIComponent(data[it]) +        
         '&' 
         }               
          return ret          
     }        
   ]   
  }).then(res => res.data);};

The request body is Request Payload, and the request header Content-Type: application/json is requested in this way. @RequestParam cannot receive parameters in the background. You can only use @RequestBody or implement spring's HandlerMethodArgumentResolver  interface to customize the parameters. The parser receives the parameters individually.

The request body format of Request Payload is a string in json format:
{"loginName": "admin", "password": "123456"}

2.Form Data

When a POST request is initiated, if the content-type is not specified, the default content-type is application/x-www-form-urlencoded. That is, the parameters will be passed in the form of Form Data and will not appear explicitly in the request url.
Insert picture description here

Form Data request:
Write picture description here

The format of the request body of Form Data is key=value&key1=value2:
loginName=admin&password=123456

Background processing:
For Request Payload requests, @RequestBody must be added to parse the request body into the corresponding bean, and the request body content can only be obtained through request.getReader()
Write picture description here

For Form Data requests, there is no need for any annotations, springmvc will automatically use MessageConverter to parse the request parameters to the corresponding beans, and request parameters can be obtained through request.getParameter(...) or received through @RequestParam

3.Query String Parameters

Query String Parameters When a GET request is initiated, the parameters will be passed in the form of url string. That is, the string after the? Is its request parameter, with & as the separator. The following http request message headers:
headers:
Insert picture description here
incoming parameters:

Insert picture description here

 

 

 

 

 

Guess you like

Origin blog.csdn.net/AN0692/article/details/109675466