2017-05-10 1 views
0

urllib3을 사용하여 python3에 HTTP 요청을 보내려고합니다.TypeError : 바이트를 str에 연결할 수 없습니다. python3

여기에 코드

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' 

사람이 내가 잘못 무엇을 지적 할 수 있습니까?

편집

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

관련 문제