lou cjcj
lou cjcj

Reputation: 33

Make multipart/form-data post requests with python

Im trying To Make post requests (multipart/form-data) in this website https://gofile.io/?t=api

every time time i got an error when i try to upload file

my code

import requests
req = requests.session()
files= {'file': open("test.txt", 'rb')}
response = req.post('https://srv-file7.gofile.io/upload', files=files)
print(response.text)

I got error every time ,are smething missing in the code

Upvotes: 1

Views: 450

Answers (1)

Balaji Kotni
Balaji Kotni

Reputation: 33

from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
import json

mp_encoder = MultipartEncoder(
    fields={
        'filesUploaded': ('file_name.txt', open('file_name.txt', 'rb'))
    }
)
r = requests.post(
    'https://srv-file9.gofile.io/upload',
    data=mp_encoder,
    headers={'Content-Type': mp_encoder.content_type}
)
scrap = r.json()
# send "Token file" 123Abc
Token = scrap['data']['code']

Check this. It will work.

Upvotes: 1

Related Questions