尝试在Python中使用REST API在Atlassian Bitbucket中创建提取请求

乔·史密斯

我正在尝试使用请求模块使用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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何:[REST]以编程方式使用REST API获取和设置Atlassian Stash(Bitbucket Server)中存储库的默认审阅者

Atlassian Connect-Express:JIRA插件中的JIRA REST API身份验证

如何通过REST API从Bamboo Atlassian构建计划中检索我的自定义变量

尝试在Java SpringBoot中使用REST API时收到错误请求

如何使用 JMeter 和 REST API 更新 Atlassian Confluence Wiki

在Python中使用Parse.com REST API创建应用

Atlassian API在API中附加文件时引发错误

从Python中的API请求中提取数据

在Java中使用BitBucket REST API

尝试使用REST API在sonatype Nexus中创建存储库

在Python中的API请求中使用变量

如何在REST API中使用提取?

转换$ .ajax请求以在异步等待中使用提取API(并尝试捕获?)

使用JSON在python中创建Rest Api?

如何使用GOLang通过调用REST API在BitBucket中创建存储库

尝试在Python中使用API JSON输出

您如何使用他们的REST API向Atlassian合流发布评论?

NoClassDefFoundError尝试在Android Studio中使用FatSecret REST API

尝试使用REST API创建Devops Agent池

在Java中使用Rest API删除Bitbucket Branch

我正在尝试存储从 Rest API 获取的授权令牌,然后在 Flutter 中其他 API 调用的标头中使用它

使用 Atlassian Confluence Python API 编辑页面时设置提交消息/更新注释

(PHP-GoDaddy) 对 Atlassian/Jira 域的 cURL API 请求失败/不返回任何内容

使用python中的请求遍历分页的REST API

REST API-在单个请求中批量创建或更新

使用访问令牌请求 Bitbucket API

在REST API中使PUT创建请求成为幂等

使用自定义SSH密钥从Atlassian Stash / BitBucket克隆

使用docker容器在Atlassian Bitbucket管道中进行测试