错误请求错误400

时间:2018-01-13 13:45:00

标签: python curl bitbucket urllib http-error

我想使用客户端配置复制以下内容。

Curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" http://localhost:7990/rest/api/1.0/projects
https://developer.atlassian.com/server/bitbucket/how-tos/example-basic-authentication/

这是下载一个文件,用于在私人仓库中更新exe。

我使用bitbucket定期收到400个错误。

我有什么:

client = Client(ClientConfig(), headers={'basic_auth':'U:P' }, refresh=True)

我想要包含此标题

header = {'Content-Type': 'application/json'}

类似于:

client = Client(ClientConfig(), headers={'basic_auth': 'brofewfefwefewef:EKAXsWkdt5H6yJEmtexN'}, header = {'Content-Type': 'application/json'}, refresh=True)

应该修复吗?

至少根据....

“某些http客户端软件希望在发送授权标头之前收到身份验证质询,这可能意味着它可能无法按预期运行。在这种情况下,您可能需要将其配置为提供如上所述的授权标头而不是依赖其默认机制。“

和...

https://stackoverflow.com/questions/8840303/urllib2-http-error-400-bad-request

对我来说,即使这次更改400 https://pastebin.com/V9ibxTRX(此处为完整代码或下方的简短版本),我总是会收到错误

可选的错误消息(它不是很漂亮,但我确实减少了它):

~~~~~ACCESSING PAGE AND FOUND!~~~
DEBUG:pyupdater.client.downloader:Url for request: https://api.bitbucket.org/2.0/repositories/Username/repository/downloads/keys.gz
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.bitbucket.org
send: b'GET /2.0/repositories/Username/repository/downloads/keys.gz HTTP/1.1\r\nHost: api.bitbucket.org\r\nAccept-Encoding: identity\r\nauthorization: Basic ywafdwafawffwawffawafwfwaawfawfg==\r\n\r\n'
reply: 'HTTP/1.1 302 Found\r\n'

~~~~~~RETRYING REDIRECTION PRESENT (IS THIS THE CAUSE OF ISSUES)??~~
DEBUG:urllib3.connectionpool:https://api.bitbucket.org:443 "GET /2.0/repositories/Username/repository/downloads/keys.gz HTTP/1.1" 302 0
DEBUG:urllib3.util.retry:Incremented Retry for (url='https://api.bitbucket.org/2.0/repositories/Username/repository/downloads/keys.gz'): Retry(total=2, connect=None, read=None, redirect=None, status=None)
INFO:urllib3.poolmanager:Redirecting https://api.bitbucket.org/2.0/repositories/Username/repository/downloads/keys.gz -> https://bbuseruploads.s3.amazonaws.com/a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/1c87431a-98de-4d97-8c80-000243f81cba/keys.gz?Signature=FvA9X7K9ryM2Ft2mTV7PZefidJY%3D&Expires=1515817377&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=6J830UBC1RFvWz.R6pMDwIiJQNKJjSkm&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): bbuseruploads.s3.amazonaws.com

~~HEADERS MIGHT BE ISSUE ACCORDING TO DOCS  https://developer.atlassian.com/server/bitbucket/how-tos/example-basic-authentication/~~

header: Server header: Vary header: Content-Type header: X-OAuth-Scopes header: Strict-Transport-Security header: Date header: Location header: X-Served-By header: ETag header: X-Static-Version header: X-Content-Type-Options header: X-Accepted-OAuth-Scopes header: X-Credential-Type header: X-Render-Time header: Connection header: X-Request-Count header: X-Frame-Options header: X-Version header: Content-Length send: b'GET /a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/1c87431a-98de-4d97-8c80-000243f81cba/keys.gz?Signature=FvA9X7K9ryM2Ft2mTV7PZefidJY%3D&Expires=1515817377&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=6J830UBC1RFvWz.R6pMDwIiJQNKJjSkm&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22 HTTP/1.1\r\nHost: bbuseruploads.s3.amazonaws.com\r\nAccept-Encoding: identity\r\nauthorization: Basic YnJvZmV3ZmVmd2VmZXdlZjpFS0FYc1drZHQ1SDZ5SkVtdGV4Tg==\r\n\r\n'
DEBUG:urllib3.connectionpool:https://bbuseruploads.s3.amazonaws.com:443 "GET /a0e395b6-0c54-4efb-9074-57ec4190020b/downloads/1c87431a-98de-4d97-8c80-000243f81cba/keys.gz?
Signature=FvA9X7K9ryM2Ft2mTV7PZefidJY%3D&Expires=1515817377&AWSAccessKeyId=AKIAIQWXW6WLXMB5QZAQ&versionId=6J830UBC1RFvWz.R6pMDwIiJQNKJjSkm&response-content-disposition=attachment%3B%20filename%3D%22keys.gz%22 HTTP/1.1" 400 None

~~UNABLE TO ACCESS PAGE (WAIT, BEFORE IT HAD 'HTTP/1.1 302 Found\r\n' SEE TOP)~~
reply: 'HTTP/1.1 400 Bad Request\r\n'

DEBUG:pyupdater.client.downloader:Resource URL: https://api.bitbucket.org/2.0/repositories/Username/repository/downloads/keys.gz
DEBUG:pyupdater.client.downloader:Got content length of: None
DEBUG:pyupdater.client.downloader:Content-Length not in headers
DEBUG:pyupdater.client.downloader:Callbacks will not show time left or percent downloaded.
DEBUG:pyupdater.client.downloader:Using file as storage since the file is too large

1 个答案:

答案 0 :(得分:0)

我不熟悉您为client对象使用的库,但您应该能够在headers字典中设置Content-Type标头。

headers = {
    'basic_auth': 'brofewfefwefewef:EKAXsWkdt5H6yJEmtexN', 
    'Content-Type': 'application/json'
}
client = Client(ClientConfig(), headers=headers, refresh=True)

不幸的是,这似乎不可能,因为headers被传递(解包)到urllib3.util.make_headers并且它不接受content_type参数。

此外,您无法访问FileDownloader._http.headers中的Client,因为它是一个本地变量。
可能的FileDownloader' hack':

class FileDownloader(object):
    ...Line 152...
    def _get_http_pool(self, secure=True):
        if secure:
            _http = urllib3.PoolManager(cert_reqs=str('CERT_REQUIRED'), 
                                        ca_certs=certifi.where())
        else:
            _http = urllib3.PoolManager()

        if self.headers:
            content_type = self.headers.get('Content-Type') 
            if 'Content-Type' in self.headers:
                del self.headers['Content-Type']
            _headers = urllib3.util.make_headers(**self.headers)
            _http.headers.update(_headers)
            if content_type:
                _http.headers['content-type'] = content_type
        print(_http.headers)
        return _http

这应该允许您在Client中传递Content-Type标头。
我不建议修改你的库的源代码,但如果你别无选择......