microsoft / microsoft/azure-devops-python-api

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

Open
#448 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
684
Forks
218
Avg merge
8d 10h
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with create_push() in azure/devops/released/git/git_client_base.py and compare the serialized request shown in the issue with the Azure DevOps REST API "Pushes - Create" documentation. Trace the call from lambda_function.py and determine which request input produces the invalid JSON error; done means a clear, reproducible explanation of the invalid field or payload.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, git, python
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.