Understand how parameters are passed for GET, POST, PATCH, and DELETE requests

Insert image description here

Understand how parameters are passed for GET, POST, PATCH, and DELETE requests

This article will show you how to pass parameters when using GET, POST, PATCH, and DELETE requests. With detailed explanations of how parameters are passed for each request and sample code, you'll learn how to properly send data to and interact with the server.

Parameter passing method of GET request

In a GET request, parameters can be passed in two ways: query string and path parameters.

Query string: Use ? plus parameter key-value pairs in the URL to pass parameters. For example, https://api.example.com/users?name=John&age=25. On the front end, you can use Axios' params property to pass query string parameters. The sample code is as follows:
axios.get(‘https://api.example.com/users’, { params: { name: 'John', age: 25 } }) In a POST request, parameters can be passed in various formats through the request body: form data, JSON data, text, etc. Parameter passing method of POST request) axios.get( Path parameters: Embed parameters into the path of the URL. For example, https://api.example.com/users/123. On the front end, you can use Axios' template literals to pass path parameters. The sample code is as follows:






https://api.example.com/users/${userId}

Form data: Pass parameters in application/x-www-form-urlencoded format. On the front end, you can use Axios's data attribute to pass form data parameters. The sample code is as follows:
axios.post(‘https://api.example.com/users’, { name: 'John', age: 25 }) JSON data: Pass parameters via application/json format. On the front end, you can use Axios's data attribute to pass JSON data parameters. The sample code is as follows: axios.post(‘https://api.example.com/users’, { headers: { ‘Content-Type’: ‘application/json’ }, data: { name: 'John', age: 25 } }) For PATCH and DELETE requests, the parameters are usually included in the request body (Request Body), and the passing method is the same as in the POST request mentioned above. Parameters of PATCH and DELETE requests Passing method axios.post('https://api.example.com/users', 'John') Text: Pass parameters in text/plain format. On the front end, you can pass parameters directly to the data property of Axios. The sample code is as follows:
















The sample code is as follows:

// PATCH 请求
axios.patch(https://api.example.com/users/${userId}, {
name: ‘John’,
age: 25
})

// DELETE request
axios.delete(https://api.example.com/users/${userId})

Summarize

In this article, you learned how to pass parameters when using GET, POST, PATCH, and DELETE requests. For GET requests, you can use query strings and path parameters; for POST requests, you can use form data, JSON data, and text; and for PATCH and DELETE requests, parameters are usually included in the request body and passed in the same way.

You can choose the appropriate parameter passing method according to your needs, and use the relevant properties of Axios to correctly send data to the server and interact with it. Remember to always use a secure transport protocol and use appropriate authentication when sending sensitive data.

Guess you like

Origin blog.csdn.net/haodian666/article/details/134887816