Skip to content
Advertisement

Invalid Json error when Uploading/pushing a file to Azure Devops Repo using Pushes Create Api with Python

I was trying to automate the task of pushing some files to various folder in a repo. I tried using Rest API provided by azure. When using Pushes Create API for the same, from the docs this is the content in the request body

snapshot of request body

This is the snapshot of python code that I wrote:

The code that I am using

In the above code repositeries_url contains the valid API URL. When I run the code I am getting response code 400 and printing the JSON gives me

{‘$id’: ‘1’, ‘innerException’: None, ‘message’: ‘The body of the request contains invalid Json.rnParameter name: contentStream’, ‘typeName’: ‘Microsoft.TeamFoundation.SourceControl.WebServer.InvalidArgumentValueException, Microsoft.TeamFoundation.SourceControl.WebServer’, ‘typeKey’: ‘InvalidArgumentValueException’, ‘errorCode’: 0, ‘eventId’: 0}

Why is this error coming and how can I rectify this error?

Advertisement

Answer

I found below mistake in your python code.

In your code, you defined payload["commits"] as commits array.

enter image description here

But you mistakenly assign temp as its value. You need to append temp to payload["commits"] array. ie. payload["commits"].append(temp)

enter image description here

Also if you want to use json in the request.post method to post the data. You can directly pass the payload object. Like below:

response = requests.post(url = repo_url, json = payload, headers = header)

Or you can use data in request.post method to post the json string. See below:

response = requests.post(url = repo_url, data= jsonPayload, headers = header)

See below full code:

....    
temp["changes"].append(aksh)
payload["commits"].append(temp)  ##append temp to commits array
jsonPayload=json.dumps(payload)

#pass payload to json parameter directly
response = requests.post(url = repo_url, json= payload, headers = header)
#you can also use data
#response = requests.post(url = repo_url, data= jsonPayload, headers = header)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement