Skip to content

azure.devops.exceptions.AzureDevOpsServiceError: The body of the request contains invalid Json. Parameter name: contentStream #448

@AndreasLuckert

Description

@AndreasLuckert

Background:

I'm using the azure-devops-python-api - library in order to programmatically push a commit to my Azure DevOps organization (git) repository.

The DevOps REST API docs for "Pushes - Create" did not help me to figure out the solution.

This similar discussion did also not resolve my problem.

Problem:

Bad request error (HTTP 400) when calling create_push() in module azure\devops\released\git\git_client_base.py.

I would like understand what exactly goes wrong here.
The error message is very generic, I could not determine which of the inputs are the problem.


Details:

In the following, I will post the whole code involved at the very bottom, but before I will detail the error traceback and the HTTP-request-details.

The entire exception traceback is:

Exception has occurred: AzureDevOpsServiceError       (note: full exception trace is shown but execution is paused at: <module>)

The body of the request contains invalid Json.
Parameter name: contentStream

  File "C:\Users\username\Projects\project\ics-venv\Lib\site-packages\azure\devops\client.py", line 270, in _handle_error
    raise AzureDevOpsServiceError(wrapped_exception)
  File "C:\Users\username\Projects\project\ics-venv\Lib\site-packages\azure\devops\client.py", line 68, in _send_request
    self._handle_error(request, response)
  File "C:\Users\username\Projects\project\ics-venv\Lib\site-packages\azure\devops\client.py", line 104, in _send
    response = self._send_request(request=request, headers=headers, content=content, media_type=media_type)
  File "C:\Users\username\Projects\project\ics-venv\Lib\site-packages\azure\devops\released\git\git_client_base.py", line 1555, in create_push
    content=content)
  File "C:\Users\username\Projects\project\src\lambda_functions\json_automation\lambda_function.py", line 251, in create_feature_branch
    project=self._project_name,
  File "C:\Users\username\Projects\project\src\lambda_functions\json_automation\lambda_function.py", line 592, in lambda_handler
    create_branch_response = pr_handler.create_feature_branch()
  File "C:\Users\username\Projects\project\src\packages\ics_lambda\__init__.py", line 31, in run_local
    result = lambda_handler(event, context=context)
  File "C:\Users\username\Projects\project\src\lambda_functions\json_automation\lambda_function.py", line 615, in <module> (Current frame)
    ics_lambda.run_local(lambda_handler, event=test_event)
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.7.9\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.7.9\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
    
azure.devops.exceptions.AzureDevOpsServiceError: The body of the request contains invalid Json.
Parameter name: contentStream

The request.body is:

{
  "commits": [{ "commitId": "12312314123123123213123123123" }],
  "refUpdates": [
    {
      "name": "target-branch",
      "oldObjectId": "12312314123123123213123123123",
      "repositoryId": "project_x"
    }
  ],
  "repository": {
    "defaultBranch": "feature-branch",
    "parentRepository": {
      "name": "project_x",
      "project": { "id": "project_x", "name": "project_x" }
    },
    "project": { "id": "project_x", "name": "project_x" }
  }
}

The request headers are:

{
  "Content-Type": "application/json; charset=utf-8",
  "Accept": "application/json;api-version=5.1",
  "X-TFS-FedAuthRedirect": "Suppress",
  "X-VSS-ForceMsaPassThrough": "true",
  "X-TFS-Session": "..."
}

The request URL is:

https://dev.azure.com/organization/project_x/_apis/git/repositories/project_x/pushes

Source Code:

The code involved until invoking the git_client.create_push() - method, is:

from azure.devops.v5_1.git.models import (
    GitCommitRef,
    GitPush,
    GitRefUpdate,
    GitRepositoryRef,
    GitRepository,
    TeamProjectReference,
)
from azure.devops.connection import Connection


release_head_commit_id = '12312314123123123213123123123'
project_name = 'project_x'
repo_name = 'project_x'
release_branch_name = 'target-branch'
feature_branch_name = 'feature-branch'
git_client = Connection(base_url=organization_url, creds=credentials).clients.get_git_client()

project_reference = TeamProjectReference(
    abbreviation=None,
    default_team_image_url=None,
    description=None,
    id=project_name,
    last_update_time=None,
    name=project_name,
    revision=None,
    state=None,
    url=None,  # potentially pass the URL to the git repo
    visibility=None,
)

parent_repo = GitRepositoryRef(
    collection=None,
    id=None,
    is_fork=None,
    name=repo_name,
    project=project_reference,
    remote_url=None,
    ssh_url=None,
    url=None,
)

git_repo = GitRepository(
    _links=None,
    default_branch=release_branch_name,
    id=None,
    is_fork=None,
    name=None,
    parent_repository=parent_repo,
    project=project_reference,
    remote_url=None,
    size=None,
    ssh_url=None,
    url=None,
    valid_remote_urls=None,
    web_url=None,
)

commit_refs: List[GitCommitRef] = [
    GitCommitRef(
        _links=None,
        author=None,
        change_counts=None,
        changes=None,
        comment=None,
        comment_truncated=None,
        commit_id=release_head_commit_id,
        committer=None,
        parents=None,
        push=None,
        remote_url=None,
        statuses=None,
        url=None,
        work_items=None,
    ),
]

ref_updates: List[GitRefUpdate] = [
    GitRefUpdate(
        repository_id=repo_name,
        name=feature_branch_name,
        old_object_id=release_head_commit_id,
    ),
]

push_obj = GitPush(
    _links=None,
    date=None,
    push_correlation_id=None,
    pushed_by=None,
    push_id=None,
    url=None,
    commits=commit_refs,
    ref_updates=ref_updates,
    repository=git_repo,
)

git_client.create_push(
    push_obj,
    repo_name,
    project=project_name,
)

PS:
Since there is no easy-to-use documentation for the python API, I copied the __init__ - args for each class instantiation, where all args are set to None by default.
As it is not made clear whether or not they are required, I left them visible for the moment. But the plan is to remove them from the code once I figured out which of them I need to use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions