One article to understand the transmission method and data format of front-end and back-end request parameters and the relationship between them

Article Directory

1. When the front-end and back-end interfaces are connected, the front-end can use the following four methods to pass parameters

2. Data format

3. The relationship between transmission methods and data formats

Summarize


1. When the front-end and back-end interfaces are connected, the front-end can use the following four methods to pass parameters

  • Query String Parameters: Add parameters to the URL as query strings, eg  http://example.com/api/users?id=123&name=John. This method is usually used for GET requests, but can also be used for POST requests.
  • Request Body Parameters: Put parameters in the request body, usually for POST, PUT or PATCH requests. In the request body, various data formats can be used, such as JSON, URL-encoded or plain text.
  • Path parameters: Use parameters as part of the URL path, usually for RESTful APIs. For example, if you have a user resource, you can use  /api/users/{id} to get or update user information, where  {id} is the path parameter.
  • Header parameters: Put parameters in HTTP headers, usually used to pass metadata or authentication information. For example, a header can be used  Authorization to pass an access token.

If you want to pass a large number of parameters, you can use request body parameters; if you want to pass sensitive information, you can use header parameters to encrypt the transmission; if you need to query data, you can use query string parameters.

2. Data format

JSON, URL encoding format, and plain text format are all common data formats used by the front-end to pass parameters. Their differences are as follows:

  • JSON: JSON (JavaScript Object Notation) is a lightweight data exchange format that is often used for front-end and back-end interactions. The JSON format supports complex data structures such as objects, arrays, strings, numbers, and Boolean values. In JavaScript, you can use  JSON.parse() and  JSON.stringify() methods to parse and generate JSON data.
  • URL encoding format: URL encoding format is a method of converting a string into a readable format of ASCII code, which is often used to pass URL parameters or form data. In URL-encoded format, special characters (such as spaces, &, +, /, etc.) will be converted to plus the  % corresponding ASCII codes, such as spaces will be converted to  %20. In JavaScript, you can use  encodeURI() the and  encodeURIComponent() methods to convert a string to URL-encoded format.
  • Plain text format: Plain text format is the most basic text format and contains no formatting or metadata. When passing parameters, you can pass the parameter value as plain text. For example,  fetch() when using a method to send a POST request, you can set the type of the request body to plain text.

If you want to pass complex data structures, you can use JSON format; if you want to pass URL parameters, you can use URL-encoded format; if you want to pass simple text data, you can use plain text format.

3. The relationship between transmission methods and data formats

  • The data formats of query string parameters and path parameters are usually fixed and cannot be freely specified. In query string parameters, the data format is usually  key=value of the form, eg  http://example.com/api/users?id=123&name=John. In path parameters, the data format usually uses placeholders  {} to represent dynamic parameters, eg  /api/users/{id}. These data formats are fixed, and multiple data formats cannot be used.
  • In the request body parameters, the data format can be specified according to specific needs. Common data formats include JSON, XML, URL encoding format, plain text format, etc. When using POST, PUT or PATCH requests, you can put parameters in the request body and use different Content-Type headers to specify different data formats. For example, headers can be used when using the JSON format  Content-Type: application/json , and headers can be used when using the URL-encoded format  Content-Type: application/x-www-form-urlencoded .


Summarize

Therefore, request body parameters are usually more flexible than query string parameters and path parameters, and can adapt to more data format requirements. But at the same time, request body parameters also require more data transfer, which may affect performance. When choosing a parameter passing method, you need to make a trade-off according to your specific needs.

Guess you like

Origin blog.csdn.net/weixin_54079103/article/details/131938548