XiaoMi/open-falcon

Python 调用POST类型api问题

xzGithu opened this issue · 2 comments

使用 urllib2请求报错,post数据要怎么写?
File "/usr/lib64/python2.7/urllib2.py", line 558, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 400: Bad Request
`
def createdir(url):

req = urllib2.Request(url=url,data=json.dumps(dd),headers=headers)
try:
    response = urllib2.urlopen(req)
    print response.read()
except urllib2.HTTPError as e:
    print e.reason,e.code

`

使用httplib post数据报错如下
{"error":"Key: 'APICreatePluginInput.GrpId' Error:Field validation for 'GrpId' failed on the 'required' tag\nKey: 'APICreatePluginInput.DirPaht' Error:Field validation for 'DirPaht' failed on the 'required' tag"}


get类型的可以正常获得
`
def listuser(url):

    request = urllib2.Request(url)
    for i in headers:
        request.add_header(i,headers[i])
    response = urllib2.urlopen(request)
    result = response.read()
    print result

`

{"error":"Key: 'APICreatePluginInput.GrpId' Error:Field validation for 'GrpId' failed on the 'required' tag\nKey: 'APICreatePluginInput.DirPaht' Error:Field validation for 'DirPaht' failed on the 'required' tag"}

将你字典中的对应的key名称改为'GrpId' 和'DirPaht' 试试

post过来的数据中,确定是否有hostgroup_id和dir_path字段,这两个字段分别对应错误提示里描述的GrpId和DirPaht,看错误提示是缺少这两个字段