0
 $ curl -F myfile=@myfilename -F 'data={"title":"some title","otherinfo" : "aabcdef"}' https://someurl

The above is the working and correct way for doing from terminal.

I tried to implement this in python using requests in this way :

files = {'myfile': open('myfilename', 'rb')}
data = {}

data['data'] = {
    'title' : 'some title',
    'otherinfo' : 'other info'
}

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

Here, the data is not reaching the destination properly, where am I wrong ?

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
user1767962
  • 2,089
  • 4
  • 18
  • 19
  • What does it mean *is not reaching the destination properly*? What does server receive when you use curl and what does it receive when you use Requests? – Piotr Dobrogost Nov 09 '12 at 21:36
  • when using curl, reaching correctly, but when using requests, the server not recognising data bcoz it wants the data to be in proper form. If u see from curl, data is like -F 'data=...' . I want its equivalent using requests. I tried sending the exact string 'data=..' like data="same as curl strng" but this requests not allowing like that, i tried json.dumps(data), but no use – user1767962 Nov 09 '12 at 21:43

1 Answers1

0

When you use -F argument curl sends data with Content-Type set to multipart/form-data whereas when you use data parameter Requests uses Content-Type set to application/x-www-form-urlencoded. You should instruct Requests to send data with the right Content-Type. See How to send a "multipart/form-data" with requests in python? how to do this.

Community
  • 1
  • 1
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366