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

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?
Programming Feeds
Learn something new everyday on Devsheet