Skip to content Skip to sidebar Skip to footer

Dealing With Bad Request

I’m getting: 'HTTP/1.1 400 Bad Request\r\n' and I don’t get why. It looks like it authenticates and then there is a redirection and then it now doesn’t work. Why is this hap

Solution 1:

The BitBucket API returns 302 for the /downloads/ endpoint, so the Authorization header is carried out to the next request, while Amazon does not like that header, so it returns 400. A workaround is recreating the redirected request manually. For example: (error checking omitted)

import urllib3

http_pool = urllib3.PoolManager()
req = http_pool.urlopen(
    'GET', 'https://api.bitbucket.org/2.0/repositories/brofewfefwefewef/eee/downloads/keys.gz',
    redirect=False, headers={'Authorization': 'Basic YnJvZmV3ZmVmd2VmZXdlZjpFS0FYc1drZHQ1SDZ5SkVtdGV4Tg=='})
redirected_req = http_pool.urlopen('GET', req.headers['Location'], preload_content=False)
withopen('keys.gz', 'wb') as f:
    f.write(redirected_req.read())

By the way, your access token is still usable.

Solution 2:

You need to give them access to the repository by creating a team inside bitbucket. Then you can use git --export to download files

git archive --remote=ssh://git@bitbucket.org/<your-username>/<reponame>.git <branchname><filename> --output output.tar

Of course, you need to have git authed eg. using an ssh key or similar

I don't think there is a way to make direct download links with auth in bitbucket, then you need to set that up outside bitbucket.

Solution 3:

As this is a 400, I would guess the request to Amazon’s S3 service doesn’t like and shouldn’t have the Authorization header, but requests passes it anyway when redirecting.

What you should do is using allow_redirects=False and then doing the redirect yourself, extracting the location from the response’s Location header.

Post a Comment for "Dealing With Bad Request"