Skip to content

[Bitbucket Cloud] Improper payload encoding when POSTing with 'x-www-form-urlencoded' content-type #1045

Open
@sqwxl

Description

@sqwxl

What I'm trying to do

The bitbucket cloud api offers an endpoint to commit a text file using the Content-Type: x-www-form-urlencoded header. Using curl the call looks something like:

curl -u username:password https://api.bitbucket.org/2.0/repositories/myorg/myrepo/src \
--data-urlencode '/file.txt=File content.' \
--data-urlencode 'author=Me McMyself <my@email.com>' \
--data-urlencode 'message=A commit message.' \
--data-urlencode 'branch=somebranch'

What I've tried

from atlassian.bitbucket import Cloud


session = Cloud(username=username, password=password, cloud=True)

repo = session.workspaces.get(myorg).repositories.get(myrepo)

data = {
    "/file.txt": "File content."
    "author": "Me McMyself <my@email.com>",
    "message": "A commit message.",
    "branch": "somebranch",
}

repo.post(
    "src",
    headers={"Content-Type": "application/x-www-form-urlencoded"},
    data=data
)

Expected behavior

The outgoing request should have a payload that looks like this:

/file.txt=File+content.&author=Me+McMyself+%3Cmy%40email.com%3E&message=A+commit+message.&branch=somebranch

Actual behavior

Instead, it looks like this:

'"{\"/file.txt\": \"File content.\", \"message\": \"A commit message.\", \"author\": \"Me McMyself <my@email.com>\", \"branch\": \"somebranch\"}"'

...which isn't correctly interpreted by bitbucket because they are expecting a urlencoded payload as above.

Explanation

AtlassianRestAPI's request method has the following lines

if files is None:
    data = None if not data else dumps(data)
    json_dump = None if not json else dumps(json)

which run before the request is passed along to the requests module...

Workaround

After trying all kinds of variations using this library, I ended up making a call with requests directly:

requests.request(
    "POST",
    repo.url + "/src",
    data=data,
    auth=(session.username, session.password),
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bitbucketAtlassian Bitbucket (Cloud or Server)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions