我有一个REST API,我正在尝试使用它,但是收到错误。在linux命令行上直接运行它可以工作。以下是API ##
curl -v -s --insecure -X POST -H "X-Auth-User: <groupApiId>" -H "X-Auth-Key: <authKey>" -H "X-Auth-Requester: <requesterCorpId>" -H "Content-Type: application/json" -d "{\"serverList\":[\"xxxxxxxx\"],\"ticket\":\"IM154587704\",\"expirationDate\":\"2015-07-28T16:36:00\",\"}" http://xxxxx.com/filters/createFilter
下面是我在python中的代码
#/usr/bin/env python
import time
import urllib
import urllib2
headers={"X-Auth-User": "xxx",
"X-Auth-Key": "xxxxxxxxxxxxxxxxxxxxxx",
"X-Auth-Requester" : "12345",
"Content-Type": "application/json"}
header["X-Auth-Requester"]=username
body_content={"serverList":'yyyyyy',
"ticket":'1234',
"expirationDate":"2016-04-12T16:36:00"}
url="http://xxxxxxxxxx.com/filters/createFilter"
body = urllib.urlencode(body_content)
req = urllib2.Request(url, body, headers)
try:
response = urllib2.urlopen(req)
the_page = response.read()
filter_result=response.code
if filter_result==200:
print(" success\n")
except urllib2.HTTPError as e:
print e.code
答案 0 :(得分:0)
使用urllib2
import urllib2
import json
headers = {
'X-Auth-User': 'xxx',
'X-Auth-Key': 'yyyy',
'X-Auth-Requester ':'uuuu',
'Content-Type': 'application/json',
}
body = {"serverList":["xxxxx"],
"ticket":cm_or_im,
"expirationDate":"2016-04-10T16:36:00",
}
body = json.dumps(body)
url="http://xxxxxx.com/filters/createFilter"
req = urllib2.Request(url,body,headers)
try:
response = urllib2.urlopen(req)
the_page = response.read()
filter_result = response.code
if filter_result==200:
print("success\n")
except urllib2.HTTPError as e:
print e.code