尝试合并时Bitbucket API错误请求 [英] Bitbucket API bad request while trying to merge

查看:947
本文介绍了尝试合并时Bitbucket API错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

$url = "https://bitbucket.org/api/2.0/repositories/***/***/pullrequests/35/merge";

$curl1 = curl_init();   

curl_setopt($curl1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); 
curl_setopt($curl1, CURLOPT_USERPWD, "***:***");
curl_setopt($curl1, CURLOPT_HEADER, true); 
curl_setopt($curl1, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_URL, $url);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_POST, true);

echo curl_exec($curl1);

响应:

HTTP/1.1 400 BAD REQUEST Server: nginx/1.5.10 Date: Wed, 04 Mar 2015 06:03:15 GMT Content-Type: text/plain Content-Length: 11 Connection: keep-alive X-Served-By: app19 X-Render-Time: 0.0410010814667 Content-Language: de X-Static-Version: 572a80470390 Vary: Authorization, Accept-Language, Cookie X-Version: 1d224fb664b6 ETag: "825644f747baab2c00e420dbbc39e4b3" X-Request-Count: 27 X-Frame-Options: SAMEORIGIN Bad Request

为什么这样无效? (出于安全考虑,我用***替换了一些信息)

Why does this not work? (For safety reasons i replaced some informations with ***)

推荐答案

根据 API ,这些必需参数为 owner repo_slug pull_request_id

According to the API, those mandatory parameters are owner, repo_slug and pull_request_id.

$request_body = array(
  'owner'           => 'account-name',
  'repo_slug'       => 'repo-name',
  'pull_request_id' => 35
);

因为您指定 application / json Content-Type,你需要 json_encode 上面的数组:

Because you specified application/json as your Content-Type, you need to json_encode the array from above:

curl_setopt($curl1, CURLOPT_POSTFIELDS, json_encode($request_body));

,您可以使用 bitbucket-api 库,它可以帮助您与Bitbucket API进行互动a 更容易的方式

As a side note, you could use bitbucket-api library, which can help you to interact with Bitbucket API in a more easy way.

接受使用该库的拉取请求,看起来像这样:

Accepting a pull request using that library, looks something like this:

$pull = new Bitbucket\API\Repositories\PullRequests();

// set your login credentials here
$pull->getClient()->addListener(
  new \Bitbucket\API\Http\Listener\BasicAuthListener('username', 'password')
);
$pull->accept($account_name, $repo_slug, 35);

您可以在 docs

免责声明:我是bitbucket-api库的作者。 >

Disclaimer: I am the author of bitbucket-api library.

这篇关于尝试合并时Bitbucket API错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆