我正在尝试使用请求模块使用Python脚本自动创建请求请求。
我可以在Linux上通过curl发出请求,它可以正常工作
curl -u devops:password -H "Content-Type: application/json" -X POST -d '{"title":"Test pull request","description":"Description Here","state":"OPEN","open":true,"closed":false,"fromRef":{"id":"refs/heads/feature","repository":{"slug":"joe","name":null,"project":{"key":"SAN"}}},"toRef":{"id":"refs/heads/master","repository":{"slug":"joe","name":null,"project":{"key":"SAN"}}},"locked":false,"reviewers":[{"user":{"name":"jmsmith"}}]}' http://stashdev.blah.com/rest/api/1.0/projects/SAN/repos/joe/pull-requests
当我尝试通过python中的Requests模块执行完全相同的操作时,它不起作用
import requests
from requests.auth import HTTPBasicAuth
url = 'http://stashdev.blah.com/rest/api/1.0/projects/SAN/repos/joe/pull-requests'
headers = {'content-type': 'application/json'}
json_data = '{"title":"Test pull request","description":"Description Here","state":"OPEN","open":true,"closed":false,"fromRef":{"id":"refs/heads/feature","repository":{"slug":"joe","name":null,"project":{"key":"SAN"}}},"toRef":{"id":"refs/heads/master","repository":{"slug":"joe","name":null,"project":{"key":"SAN"}}},"locked":false,"reviewers":[{"user":{"name":"jmsmith"}}]}'
r = requests.post(url, headers=headers, json=json_data, auth=HTTPBasicAuth('devops', 'password'))
返回的json是:
{u'errors': [{u'message': u'Can not instantiate value of type [map type; class com.atlassian.bitbucket.rest.pull.RestPullRequest, [simple type, class java.lang.String] -> [simple type, class java.lang.Object]] from JSON String; no single-String constructor/factory method', u'exceptionName': u'org.codehaus.jackson.map.JsonMappingException', u'context': None}]}
谁能帮助我了解这里发生了什么,为什么这不起作用?
您的json数据无效。在请求中使用“ json”参数时,您需要发布字典对象而不是字符串。
import requests
from requests.auth import HTTPBasicAuth
url = 'http://stashdev.blah.com/rest/api/1.0/projects/SAN/repos/joe/pull-requests'
headers = {'content-type': 'application/json'}
json_data = {
"title":"Test pull request",
"description":"Description Here",
"state":"OPEN",
"open":True,
"closed":False,
"fromRef":{
"id":"refs/heads/feature",
"repository":{
"slug":"joe",
"name":None,
"project":{
"key":"SAN"
}
}
},
"toRef":{
"id":"refs/heads/master",
"repository":{
"slug":"joe",
"name":None,
"project":{
"key":"SAN"
}
}
},
"locked":False,
"reviewers":[
{"user":{
"name":"jmsmith"
}
}
]
}
r = requests.post(url, headers=headers, json=json_data, auth=HTTPBasicAuth('devops', 'password'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句