Rob
Rob

Reputation: 15

Convert CURL command to POST Python requests with form parameter

I'm a requests/python noob.I've figured out GET requests no problem but can't find out how to code those -F parameters properly with POST

http://docs.python-requests.org/en/master/user/quickstart/

I'm trying to figure out how I can make requests take care of a POST request that includes form data?

curl -X POST "https://falcon-sandbox.com/api/v2/submit/file?_timestamp=1548810863364" -H  "accept: application/json" -H  "user-agent: Falcon Sandbox" -H  "api-key:xxx" -H  "Content-Type: multipart/form-data" -F "[email protected];type=application/pdf" -F "environment_id=160"

Upvotes: 1

Views: 2752

Answers (1)

Attila Kis
Attila Kis

Reputation: 533

import requests

session = requests.Session()

headers = {'accept' : 'application/json',
           'user-agent': 'Falcon Sandbox',
           'api-key':'xxx',
           'Content-Type': 'multipart/form-data'}

data = {'enviroment_id' : '160'}
files = {"file": open('test.pdf', "rb")}


session.post("https://falcon-sandbox.com/api/v2/submit/file", headers = headers, data = data, files = files)

Hopefully I haven't missed anything, but the structure should be the same.

Upvotes: 2

Related Questions