Home > OS >  Convert curl send file to python request
Convert curl send file to python request

Time:01-21

For anonfiles, they only give the curl function to upload a file,

curl -F "[email protected]" https://api.anonfiles.com/upload

and I want to use python requests for this.

I tried

import requests
data = requests.put(url = "https://api.anonfiles.com/upload",data=open('file.txt','r').read() )
print(data.text)

CodePudding user response:

Try:

import requests

with open('test.txt', 'rb') as fp:
    r = requests.post('https://api.anonfiles.com/upload', files={'file': fp})
print(r.json())

Output:

{
    "status": true,
    "data": {
        "file": {
            "url": {
                "full": "https://anonfiles.com/oa28JcS2ya/test_txt",
                "short": "https://anonfiles.com/oa28JcS2ya"
            },
            "metadata": {
                "id": "oa28JcS2ya",
                "name": "test.txt",
                "size": {
                    "bytes": 580,
                    "readable": "580 B"
                }
            }
        }
    }
}

More information here: POST a Multipart-Encoded File

  • Related