requests.post
时间: 2023-09-26 18:11:20 浏览: 79
python requests.post带head和body的实例
requests.post is a method in the Python requests library that is used to send an HTTP POST request to a specified URL. It allows for sending data in the request body, setting headers, and specifying the request timeout. The response from the server can be accessed as a response object, which contains information about the status code, headers, and content of the response.
Example usage:
```
import requests
url = 'https://example.com/api/v1/user'
data = {
'name': 'John',
'email': 'john@example.com'
}
headers = {
'Authorization': 'Bearer my_token',
'Content-Type': 'application/json'
}
response = requests.post(url, json=data, headers=headers)
print(response.status_code)
print(response.json())
```
In this example, we are sending a POST request to the URL 'https://example.com/api/v1/user' with the data in the JSON format and the authorization token in the header. The response status code and JSON content are printed to the console.
阅读全文