Search code snippets, questions, articles...

Send post request with data and headers Python requests

You can send HTTP requests using the python package requests. Also, you can send form data, raw data, and headers in the HTTP request.
import requests

raw_json_data = { "user_name": "hello", "password": "password1" }
data = { "first_name": "hello", "last_name": "world" }
headers = { 'Authorization': 'Bearer token_value_here' }

response ='https://path/to/api/url/endpoint', headers = headers, data = data, json=raw_json_data)

print(response.status) #prints the status of request
print(response.content) #prints the content
print(response.json()) #json formatted data if response returns any
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

In the code snippet, we are sending a request using the python-requests package. You can install the requests package in your python project using the below command

pip install requests
Was this helpful?