0

My code is:

headers={"Content-type": "application/x-www-form-urlencoded","Accept": "multipart/form-data"}
cox = httplib.HTTPConnection("somedomain.com")
params=urllib.urlencode( {'par1':'1','par2':'2'})
cox.request("POST","/cportal/public/api/edit?par3=3&par4=4",params,headers)

along two POST parameters par1 and par2, I want to send a file. how can I do that?

  • 1
    first things first, i always use requests whenever possible. they make it more straight forward – SuperStew May 10 '18 at 17:05
  • Possible duplicate of [Send file using POST from a Python script](https://stackoverflow.com/questions/68477/send-file-using-post-from-a-python-script) – Barmar May 10 '18 at 17:08
  • Thanks Barmar, but this script sends only file, I want to POST file and parameters – user3260061 May 10 '18 at 17:11

1 Answers1

1

using requests

import requests
site = 'site.com'
filename = 'image.jpg'

At The Place Of 'Image' , Add The ID Of The Upload File IN The Site:

file={'image':(filename, open(filename, 'r'),'multipart/from-data')}
data = {
      "Button" : "Submit" # Button ID on html
}
r = requests.post(site, files=file, data=data)
Skiller Dz
  • 897
  • 10
  • 17