Sending data Curl/Json in Python

前端 未结 4 1638
遥遥无期
遥遥无期 2021-01-13 06:13

I`m trying to make those 2 requests in python:

Request 1:

 curl -X POST -H \"Content-Type: application/json\" -d \'{ \"auth_token\": \"auth1\", \"w         


        
4条回答
  •  旧时难觅i
    2021-01-13 07:03

    Requests provides you with the simplest and yet (very) powerful way to deal with HTTP requests in Python.

    Maybe try something like this:

    import requests
    import simplejson as json
    
    url = "http://ip:port"
    data = {'auth_token': 'auth1', 'widget': 'id1', 'title': 'Something1', 'text': 'Some text', 'moreinfo': 'Subtitle'}
    headers = {'Content-type': 'application/json'}
    r = requests.post(url, data=json.dumps(data), headers=headers)
    

    If the API requests authentication:

    r = requests.post(url, data=json.dumps(data), headers=headers, auth=('user', 'pass'))
    

    See [Requests auth] for details.

提交回复
热议问题