2017-05-10 6 views
0

urllib3を使用してpython3でHTTPリクエストを送信しようとしています。ここでTypeError:strにバイトを連結できませんpy3 3

は、コードスニペット

request_body = {'grant_type':'password','username': username,'password': password} 
request_headers = {'Content-Type' : 'application/x-www-form-urlencoded','Authorization': "hash string"} 
http = urllib3.PoolManager() 
response = http.request('POST', 'https://api/url/endpoint', headers=request_headers, body=request_body) 

あるしかし、私はそれを実行しようとすると、それは次のようなエラーがスローされます。

TypeError: can't concat bytes to str

完全トレースバック

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "/Users/Mubin/anaconda/lib/python3.6/site- 
packages/urllib3/request.py", line 70, in request 
**urlopen_kw) 
File "/Users/Mubin/anaconda/lib/python3.6/site- 
packages/urllib3/request.py", line 148, in request_encode_body 
    return self.urlopen(method, url, **extra_kw) 
File "/Users/Mubin/anaconda/lib/python3.6/site- 
    packages/urllib3/poolmanager.py", line 321, in urlopen 
response = conn.urlopen(method, u.request_uri, **kw) 
File "/Users/Mubin/anaconda/lib/python3.6/site- 
    packages/urllib3/connectionpool.py", line 600, in urlopen 
chunked=chunked) 
File "/Users/Mubin/anaconda/lib/python3.6/site- 
    packages/urllib3/connectionpool.py", line 356, in _make_request 
conn.request(method, url, **httplib_request_kw) 
File "/Users/Mubin/anaconda/lib/python3.6/http/client.py", line 1239, in request 
self._send_request(method, url, body, headers, encode_chunked) 
File "/Users/Mubin/anaconda/lib/python3.6/http/client.py", line 1285, in _send_request 
self.endheaders(body, encode_chunked=encode_chunked) 
File "/Users/Mubin/anaconda/lib/python3.6/http/client.py", line 1234, in endheaders 
self._send_output(message_body, encode_chunked=encode_chunked) 
File "/Users/Mubin/anaconda/lib/python3.6/http/client.py", line 1064, in _send_output 
+ b'\r\n' 

誰も私が間違ってやっていると指摘することはできますか?

EDIT

request_body型チェック

>>> type(request_body) 
    <class 'dict'> 
>>> type(request_body['username']) 
    <class 'str'> 
>>> type(request_body['password']) 
    <class 'str'> 
>>> type(request_body['grant_type']) 
    <class 'str'> 
+1

のよう

http.request('POST', 'https://api/url/endpoint', headers=request_headers, fields=request_body) 

username'と '' password'の種類は何ですか? – Evert

+0

両方が文字列 ' ' – Mubin

+0

あなたは100%確実ですか?メッセージ本文に何かがバイト文字列 – Avantol13

答えて

2

私はあなたの代わりにbodyfieldsパラメータを使用するためのものだと思います。このexample

+0

+1でなければならないと思いますが、エラーなしでリクエストを送信していますが、' grant_type 'がリクエストに含まれていないというapiがあります。 '' {"error_description": "grant_typeパラメータの値がありません"、 "error_request"} '' – Mubin

+0

@Mubinは' grant_type'がなぜ存在しなかったのか分かりましたか? –

+0

いいえ、要請を出していた、それは主要な問題でしたが、私は何とかそれを理解することはありません.. – Mubin

関連する問題