Rob
Rob

Reputation: 3459

python sending curl request with actual data instead of file

If I want to mimic a curl request in python like:

curl https:/fake_api.com/extract.do \
    --form account=my_account \
    --form username=my_username \
    --form password=my_password \
    --form uploaded_file=@my_document.xml

how would I replicate this request in python if I don't want to actually use a file, but pass the xml from a string?

Upvotes: 0

Views: 181

Answers (1)

dfranca
dfranca

Reputation: 5322

You can use the requests library to achieve this result. the post method accepts a dictionary files containing the filenames as keys and the content as values. The follow code should work as you expect:

import requests
requests.post('https:/fake_api.com/extract.do', 
   data={
      'account': 'my_account',
      'username': 'my_username',
      'password': 'my_password'
   },
   files={'my_file.xml': '<test><a>your content</a></test>'}
)

Upvotes: 2

Related Questions