尝试在 Python 中使用 REST API 在 Atlassian Bitbucket 中创建拉取请求
Trying to create pull request in Atlassian Bitbucket using REST API in Python
我正在尝试使用请求模块使用 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 中的请求模块执行完全相同的操作时,它不起作用
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' 参数时,您需要 post 字典对象而不是字符串。
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'))
我正在尝试使用请求模块使用 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 中的请求模块执行完全相同的操作时,它不起作用
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' 参数时,您需要 post 字典对象而不是字符串。
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'))