Uploading JSON file from ServiceNow to repository using REST API

Srikanth Ramasubramanian
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 7, 2019

Hi there,

Here's what I am trying to accomplish:

> I have a script on ServiceNow that comes up with the contents of a JSON file (doesn't actually create the file). We currently have a rather elaborate architecture to get this content inserted as a JSON file into a BitBucket repo.

> I am wondering if there is a REST API provided by BitBucket that would allow me to instead create a file with content-type as JSON and insert the contents generated by my script as an XML/JSON payload.

Thanks in advance for your help.

1 answer

0 votes
Stephen Sifers
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 11, 2019

Hello Srikanth and welcome to the Community!

To upload files into Bitbucket will depend on the Bitbucket product you’re using. I will break down the answer for uploading by Cloud and Server. (This will also depend on the scripting language you’re using to compile your JSON file along with how you’re making your API calls).

For Bitbucket cloud I would suggest the following method;

  1. Take your JSON data and pipe it out to a variable.
  2. Use this variable to create a file.
  3. Once you have your JSON file, use the following API endpoint to upload it
  4. POST /2.0/repositories/{username}/{repo_slug}/src

For Bitbucket server I would suggest the following method;

  1. Take your JSON data and pipe it out to a variable.
  2. Use this variable to create a file.
  3. Once you have your JSON file, use the following API endpoint to upload it
  4. /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/browse/{path:.*}

Depending on with product you choose, you will need to have your file stored in a way that allows you to upload/PUT it into Bitbucket.

I hope this proves helpful for getting your JSON content into Bitbucket.

Regards,
Stephen Sifers

Srikanth Ramasubramanian
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 14, 2019

Hi Stephen,

Thank you for taking the time to answer my question, I am a bit confused as to how I would create the file.

The method we currently use allows us to provide the filename, extension and file content which is stored as a record on a ServiceNow table. This record is then pulled in by a ServiceNow application installed on our servers called the MID server which generates the file based on the aforementioned file attributes.

I am wondering if perhaps there is a way to instead call a BitBucket API with the JSON content as a payload thereby creating a new file in the repo for further consumption.

Please let me know if my question isn't clear and I'll provide more information.

Regards,

Srikanth

Stephen Sifers
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 18, 2019

Hello Srikanth,

Thank you for providing additional information and clarification into what you want to do.

The only endpoint that I’m able to find to upload data into Bitbucket server via the REST API is /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/browse/{path:.*} which requires a path/file to be included.  Perhaps within your script, you may convert the JSON payload into a filetype as a variable and then include that. This will all depend on the scripting language you use.

What it sounds like is that you want to commit code to Bitbucket server but use the REST API instead of Git commands to make your commit. An alternative would be to store your files in a git repository locally and script your git commits instead of using the REST API. This may not apply to how your integration is configured though.

I hope this provides helpful and gives some insight into how to upload a file and an alternative to using the REST API.

Regards,
Stephen Sifers

Robbie Nathan
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 13, 2023

I am trying to do something similar with Bitbucket Server but getting a 415.

Based on the documentation, if creating a file the source commit Id has to be left to null, which I am doing, and all the other data I am providing in the body as a json, including the file content and doing a put request to the file path on the repo. 

I am using powershell. Unfortunately the documentation isn’t very good as it only gives an example using curl and the other languages don’t really have a good example. 

also, if I wanted to get the sourceCommitId of a file, how would I do that? I don’t see an endpoint for it, just for getting the latest commit on the whole repo, as opposed to a file.

 

any help will be greatly appreciated.

 

Thnx

Rob

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events