我有一个Json请求,我正在发送django项目。我发送请求,我得到的回应正如我所期待的那样。但我认为响应会以字符串形式返回,而不是文件或可迭代文件。我不确定如何确保响应是正确的格式来处理。这是我从django views.py文件发送的请求......
def createUserSynapse(request):
data = {
"logins":[
{
"email":"test@test.com",
}
],
"phone_numbers":[
"123.456.7890",
"test@test.com",
],
"legal_names":[
"Test name",
],
"extras":{
"supp_id":"asdfe515641e56wg",
"cip_tag":12,
"is_business":False,
}
}
req = urllib.request.Request('http://uat-api.synapsefi.com')
req.add_header('X-SP-GATEWAY', 'client_id_asdfeavea561va9685e1gre5ara|client_secret_4651av5sa1edgvawegv1a6we1v5a6s51gv')
req.add_header('X-SP-USER-IP', '127.0.0.1')
req.add_header('X-SP-USER', '| ge85a41v8e16v1a618gea164g65')
req.add_header('Content-Type', 'application/json')
print(req.headers)
response = urllib.request.urlopen(req, json.dumps(data).encode('utf8'))
print(response)
return render(reqest, 'tabs/create_user_synapse.html', response)
另外,第二次到最后一次应该打印响应并且它不打印任何东西......
任何人都可以帮助我...... 这是完整的错误:
TypeError at /setup_profile/
POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
Request Method: POST
Request URL: http://127.0.0.1:8000/setup_profile/
Django Version: 1.8.6
Exception Type: TypeError
Exception Value:
POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
Exception Location: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\urllib\request.py in do_request_, line 1248
Python Executable: C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\python.exe
Python Version: 3.6.1
Python Path:
['C:\\Users\\OmarJandali\\Desktop\\opentab\\opentab',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\DLLs',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36',
'C:\\Users\\OmarJandali\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']
Server time: Tue, 5 Sep 2017 06:17:10 +0000
Traceback Switch to copy-and-paste view
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in profileSetup
createUserSynapse(request) ...
▶ Local vars
C:\Users\OmarJandali\Desktop\opentab\opentab\tab\views.py in createUserSynapse
response = urllib.request.urlopen(req, json.dumps(data)) ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\urllib\request.py in urlopen
return opener.open(url, data, timeout) ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\urllib\request.py in open
req = meth(req) ...
▶ Local vars
C:\Users\OmarJandali\AppData\Local\Programs\Python\Python36\lib\urllib\request.py in do_request_
raise TypeError(msg) ...
▶ Local vars