You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过BitBucket REST API添加Webhook遇405错误,求正确实现方案

解决BitBucket REST API添加Webhook的405错误问题

你遇到的<Response [405]>响应是因为添加新Webhook必须使用POST请求,而非PUT——PUT方法仅用于更新已存在的Webhook配置,创建操作需要用POST。以下是正确的操作步骤:

1. 确认正确的API端点

你使用的https://bitbucket.com/rest/api/1.0/projects/GGT/repos/grp1/webhooks端点格式是正确的,格式通用为:

https://bitbucket.com/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/webhooks

2. 构造POST请求的核心配置

请求体需以JSON格式提交Webhook的关键参数,必填项包括name、url和events,示例如下:

{
  "name": "代码推送触发Webhook",
  "url": "https://你的服务地址/webhook/receive",
  "description": "仓库有代码推送时触发",
  "events": ["repo:push"],
  "enabled": true
}
  • events可选值参考BitBucket支持的事件类型,比如pr:opened(合并请求创建)、repo:updated(仓库信息更新)等。
  • enabled设为true表示创建后立即启用Webhook。

3. 确保认证权限正确

沿用你获取仓库信息时的认证方式(Basic Auth或OAuth2均可),请求头需包含认证信息和Content-Type声明,示例请求头:

headers = {
    "Content-Type": "application/json",
    "Authorization": "Basic 你的Base64编码用户名密码"
}

4. Python脚本示例

替换占位符后即可直接运行:

import requests
import json

# 配置参数
project_key = "GGT"
repo_slug = "grp1"
auth = ("你的用户名", "你的应用密码")  # 或使用OAuth token
webhook_target_url = "https://你的服务地址/webhook/receive"

# 组装API地址
api_url = f"https://bitbucket.com/rest/api/1.0/projects/{project_key}/repos/{repo_slug}/webhooks"

# 请求体内容
payload = {
    "name": "代码推送Webhook",
    "url": webhook_target_url,
    "events": ["repo:push"],
    "enabled": True
}

# 发起POST请求
response = requests.post(api_url, auth=auth, json=payload)

# 处理响应
if response.status_code == 201:
    print("Webhook创建成功:", response.json())
else:
    print(f"请求失败,状态码: {response.status_code},错误信息:", response.text)

常见问题排查

  • 若仍报错,检查你的账号是否拥有该仓库的管理员权限(创建Webhook需要管理员权限)。
  • 验证请求体JSON格式是否正确,确保没有遗漏必填字段。
  • 确认目标Webhook地址可正常访问,BitBucket会对部分场景下的地址做可达性校验。

内容的提问来源于stack exchange,提问作者The Mungax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 17:30:55