将cURL转换为请求

时间:2016-01-21 20:34:01

标签: python curl

这是我的cURL,有效:

curl -k -u 'user_name:password' 'https://api.instartlogic.com/xxxx/v1/cache/purge' -d '{"purge_request":{"action":"PURGE","uris": [{"match_mode":"PREFIX_LITERAL","uri_pattern":"http://www.myurl.com"}]}}' -X POST

我正在尝试使用请求库将其转换为python脚本:

import requests
import json
from celery import task
headers = {}
auth_str = str(user_name) + ':' + str(user_passwd)
auth_str = base64.b64encode(auth_str)
auth_str = "Basic " + auth_str
headers['Authorization'] = auth_str
headers['Content-Type'] = 'application/json'
user_name = "username"
password = "password"
customer_name = "customer_name"

@task
def cache_flush(user_name=user_name, user_passwd=password, customer_name=customer_name):
    api_host = "http://api.instartlogic.com/xxxx/v1/cache/purge"

    payload = json.dumps({
        "purge_request": {
            "action": "PURGE",
            "uris": [
              {
                  "match_mode": "PREFIX_LITERAL",
                  "uri_pattern": "http://www.myurl.com"
              }
            ]
        }
     })
    re = requests.post(api_host, headers=headers, data=payload, verify=False)

但我继续使用内容'{"message":"No security context available.","type":"NotFound"}'取回404,我无法在任何地方找到有关该错误的任何信息。

1 个答案:

答案 0 :(得分:1)

尝试此代码:

import requests

data = '{"purge_request":{"action":"PURGE","uris": [{"match_mode":"PREFIX_LITERAL","uri_pattern":"http://www.myurl.com"}]}}'

response = requests.post('https://api.instartlogic.com/xxxx/v1/cache/purge', data=data, verify=False, auth=('user_name', 'password'))