lost and found ( for me ? )

Pyhon: HTTP POST with requests, pycurl module

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"}'

[ requests module ]

# cat requests_create_empty_shared_list.py
#!/bin/env python

import json
import requests

url = ''

def creatList():
       headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
       values = {

       data = json.dumps(values)
       res = requests.post(url,data=data,headers=headers,auth=requests.auth.HTTPBasicAuth('admin','admin'))


[ pycurl module ]

# cat pycurl_create_empty_shared_list.py
#!/bin/env python

import pycurl
import json

url = ''
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)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.