1

I trying to make a POST call using urllib (python 3.5), but can't figure it out. There are plenty examples on how to make either POST call without auth or call with auth, but GET... I'm struggling to put 2 and 2 together! Could someone please help me with a code?

Dror Av.
  • 1,184
  • 5
  • 14
uzla
  • 515
  • 1
  • 4
  • 20
  • https://stackoverflow.com/questions/3238925/python-urllib-urllib2-post – Dhruv Aggarwal Aug 14 '17 at 14:00
  • 1
    `auth` in `requests` is much easier. you can use that - http://docs.python-requests.org/en/master/user/authentication/. Also can you actually post your code so we have any idea what you tried? – Dror Av. Aug 14 '17 at 14:25
  • Thanks for responses, but I have to use urllib, not requests. – uzla Aug 14 '17 at 14:47
  • Thanks for the link, but I could not any reference to auth, as mentioned in my question. – uzla Aug 14 '17 at 14:48

1 Answers1

3

You can try:

import urllib.request  

auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='name',
                          uri='url',
                          user='user',
                          passwd='pass')
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
req = urllib.request.Request(url=some_url, data=data, method='POST')
urllib.request.urlopen(req)
Dror Av.
  • 1,184
  • 5
  • 14