Here is sample Python script when achieving the same “curl ..” POST command as below with python requests, pycurl module.
# curl -v -i -w "\n" -H "Accept: application/json" -H "Content-type: application/json" -u "admin:admin" -X POST -d '{"name":”hello"}' http://127.0.0.1:8080/list
|
[ requests module ]
# cat requests_create_empty_shared_list.py
#!/bin/env python
import json
import requests
url = 'http://127.0.0.1:8080/list'
def creatList():
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
values = {
'name':'foobar01'
}
data = json.dumps(values)
res = requests.post(url,data=data,headers=headers,auth=requests.auth.HTTPBasicAuth('admin','admin'))
creatList()
|
[ pycurl module ]
# cat pycurl_create_empty_shared_list.py
#!/bin/env python
import pycurl
import json
url = 'http://127.0.0.1:8080/list'
headers = {'Content-Type': 'application/json'}
payload = {'name':'hi'}
post = json.dumps(payload)
def createList():
c = pycurl.Curl()
c.setopt(pycurl.URL, '%s' % url)
c.setopt(pycurl.HTTPHEADER, ['Accept: application/json', 'Content-Type: application/json'])
c.setopt(pycurl.VERBOSE, 0)
c.setopt(pycurl.USERPWD, 'admin:admin')
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, post)
c.perform()
|
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.