You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
I'm trying to update the content of an existing snippet via the API (using Python requests). I've written plenty of other automation, but this endpoint is giving me trouble. Probably because I'm not doing multipart form-data part correctly.
import os
import requests
import json
# Authenticate
token_response = requests.post(
url="https://bitbucket.org/site/oauth2/access_token",
auth=(f"{BITBUCKET_KEY}", f"{BITBUCKET_SECRET}"),
data={"grant_type":"client_credentials"}
).json()
access_token = token_response['access_token']
# Update
url = f"https://api.bitbucket.org/2.0/snippets/{workspace}/{encode_id}"
headers = {
"Accept": "application/json",
"Content-Type": "multipart/form-data",
"Authorization": f"Bearer {access_token}"
}
files = {'my-file.md': open('my-file.md','rb')}
response = requests.request(
"PUT",
url,
headers=headers,
files=files
)
if response.status_code != 200:
print(f"Got HTTP {response.status_code} on snippet write: {response.text}")
exit(2)
And the API is returning `Bad Request (400)` without any further context. Any ideas what I'm doing wrong? :)
Thank you for reaching out to the community.
Would it be possible for you to try to use the below content-type instead?
application/json; charset='utf-8'
Based on our API 2.0 documentation here, here's how you can update a snippet's file contents and title using CURL command:
curl -X PUT -H "application/json; charset='utf-8'" -H "Authorization: Bearer <access_token>" https://api.bitbucket.org/2.0/snippets/workspace_id/encoded_id -F title="test123" -F file=@TEST.md
You can also try the CURL command above to see if the API endpoint works.
Additionally, you can add "-vvv" flag to debug the CURL command.
Hope it helps and let me know if you have further questions.
Regards,
Mark C
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.