sir__finley
sir__finley

Reputation: 170

How to convert python3 post request with both files and params to curl

I have python3 script which correctly uploaded image to site and I want to do exactly the same via curl command.

import requests


url = 'http://1.2.3.4/upload'
files = {'image': open('foo.jpg', 'rb')}
params = {'bar': 'baz'}
requests.post(url, files=files, params=params)
# incorrect
curl -XPOST -F 'image=foo.jpg' -d 'bar=baz' 'http://1.2.3.4/upload'

I expect command like this will upload foo.jpg with param baz, but it seems to -F is ignored by curl when -d is specified.

I want curl with file name specified, rather than whole file data inside command like curlify library does.

Upvotes: 0

Views: 191

Answers (1)

oguz ismail
oguz ismail

Reputation: 50815

  1. You can't use -F and -d together, former overrides the latter,
  2. You need to prefix filename with an @ sign,
  3. -F implies -X POST, and when it is used all data is sent as form-data, so bar=baz will not be appended to URL as with -G and -d, you need to append it manually.
curl -F '[email protected]' 'http://1.2.3.4/upload?bar=baz'

Upvotes: 1

Related Questions