[Python] How to post a file in python?

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I want to send a file, instead of json data to an api . I looked up some example and tried a curl command and tried requests library in python to post a file. which works. my question , is do I have to specify content type in header ? also, if there are different data types/files, I want to send , how would that work. say I have , plain text file , a json file or an image file that I want to send all at once. how can I send all of the files at once via requests library?

    import requests

    files = {'data_file': open('data.json','rb')}

    r = requests.post(url, files=files)


    curl https://example.com/post/data
    -d @data.json
    -H "Content-Type: application/javascript"

    Continue reading...

Compartilhe esta Página