Sending FORM data POST using Python Requests library

tech kamar
Jun 26, 2024

I regularly use python requests library at work. Most times, I used JSON as medium for communication between client and server.

Recently, I had a problem where i had to send FORM data to an API instead of JSON.

My backend server is hosted at http://localhost:8080

http://localhost:8080/login is the end point I want to hit with username and password as form data.

So this is how I solved

import requests
url = "http://localhost:8080/login"
form_data = {"username":"johndoe", "password": "user@1234"}
resp = requests.post(url, data=form_data)

while sending JSON data, one would use json=form_data instead in the code snippet above. That’s the difference

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response