Home > Net >  How can I do a python API request with the body?
How can I do a python API request with the body?

Time:11-30

if I do a POST request on Postman with my local API server it works:

enter image description here

But if I try in python with this syntax it doesn't work: requests.post('http://127.0.0.1:5001/api/v0/add', data={'path': 'test'}).text

it returns: "file argument 'path' is required\n"

Can you please explain me why it doesn't work?

CodePudding user response:

If I pass the files parameter instead of data or json, it works!

requests.post(url = api_url, files={'path':'test'}).text

CodePudding user response:

The issue is that using data on requests.post defaults to application/x-www-form-urlencoded while your application wants multipart/form-data. Try using files instead of data:

requests.post('http://127.0.0.1:5001/api/v0/add', files={'path': 'test'}).text
  • Related