2017-05-10 29 views
0

Ich versuche, HTTP-Anfrage in python3 mit urllib3 zu senden.TypeError: kann keine Bytes zu str python3 concat

Hier ist Code-Schnipsel

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) 

Aber wenn ich versuche, es auszuführen, es wirft folgende Fehlermeldung.

TypeError: can't concat bytes to str

Vollzurückverfolgungs

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' 

Kann jemand darauf hinweisen, was ich falsch mache?

EDIT

request_body Typprüfung

>>> 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

Was sind die Arten von 'username' und' password'? – Evert

+0

beide sind Zeichenfolgen '' – Mubin

+0

Sind Sie 100% sicher? Es scheint, dass etwas in dem Nachrichtentext ein Byte-String ist – Avantol13

Antwort

2

Ich glaube, Sie fields Parameter statt body verwenden soll.

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

wie in diesem example

+0

+1, es sendet jetzt eine Anfrage ohne Fehler, aber api sagt, dass' grant_type 'von der Anfrage fehlt. ''{" error_description ":" Missing grant_type Parameter Wert "," error ":" invalid_request "}'' – Mubin

+0

@Mubin Haben Sie herausgefunden, warum 'grant_type' fehlt? –

+0

nein, aber es war Anfrage senden, das war das Hauptproblem, weniger werde ich das irgendwie herausfinden .. – Mubin

Verwandte Themen