简体   繁体   中英

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

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

在此处输入图像描述

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM