Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

php - Bitbucket API bad request while trying to merge

This is my code:

$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);

Thats the response:

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 ***)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are missing the mandatory parameters for that particular endpoint which should be included in your request body.

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
);

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));

 

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 BitbucketAPIRepositoriesPullRequests();

// set your login credentials here
$pull->getClient()->addListener(
  new BitbucketAPIHttpListenerBasicAuthListener('username', 'password')
);
$pull->accept($account_name, $repo_slug, 35);

You can read more in the docs.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...