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
This is the snapshot of python code that I wrote:
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.
But you mistakenly assign temp as its value. You need to append temp to payload["commits"]
array. ie. payload["commits"].append(temp)
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)