问题描述
我正在启动一个必须在 Google App Engine 上部署的项目。我读到必须使用 gluon.tools fetch 函数而不是 urllib.request。
这里我有两个 defs(),第一个使用 urllib 运行正常,第二个使用 fetch 无法运行。 看起来好像数据参数格式错误。
必须如何使用这个 fetch 函数? 我一直在互联网上寻找示例,但没有找到任何东西(除了 fetchfunction 的代码)。
# -*- coding: utf-8 -*-
from gluon.tools import fetch
import json
import urllib
def gQLquery1():
data = {'query':'{me{username myMachines{name}}}'}
url = 'https://ecostruxure-machine-advisor.se.app/daas/graphql'
Bearer = "Bearer ngU5TkM5yN2m5VCBeJBQ4F3NGjuvecn8FqK7f7Fc"
data = json.dumps(data).encode('utf-8')
headers={'authorization': Bearer,'Content-Type':'application/json'}
req = urllib.request.Request(url,data=data)
req.add_header('authorization',Bearer)
req.add_header('Content-Type','application/json')
response = urllib.request.urlopen(req).read()
decoded = json.loads(response)
out = json.dumps(decoded)
return out
def gQLquery2():
data = {'query':'{me{username myMachines{name}}}'}
url = 'https://ecostruxure-machine-advisor.se.app/daas/graphql'
Bearer = "Bearer ngU5TkM5yN2m5VCBeJBQ4F3NGjuvecn8FqK7f7Fc"
#data = json.dumps(data).encode('utf-8')
headers={'authorization': Bearer,'Content-Type':'application/json'}
#req = urllib.request.Request(url,data=data)
#req.add_header('authorization',Bearer)
#req.add_header('Content-Type','application/json')
#response = urllib.request.urlopen(req).read()
response = fetch(url,data,headers )
decoded = json.loads(response)
out = json.dumps(decoded)
return out
退票:
Versión
web2py™ Version 2.21.1-stable+timestamp.2020.11.28.04.10.44
Python Python 3.7.4: D:\Oscar\_Particular\web2py\web2py_no_console.exe (prefix: D:\Oscar\_Particular\web2py)
Traceback (most recent call last):
File "D:\Oscar\_Particular\web2py\gluon\tools.py",line 4639,in fetch
from google.appengine.api import urlfetch
File "D:\Oscar\_Particular\web2py\gluon\custom_import.py",line 85,in custom_importer
return base_importer(pname,globals,locals,fromlist,level)
ModuleNotFoundError: No module named 'applications.hola'
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "D:\Oscar\_Particular\web2py\gluon\restricted.py",line 219,in restricted
exec(ccode,environment)
File "D:/Oscar/_Particular/web2py/applications/hola/controllers/default.py",line 126,in <module>
File "D:\Oscar\_Particular\web2py\gluon\globals.py",line 430,in <lambda>
self._caller = lambda f: f()
File "D:/Oscar/_Particular/web2py/applications/hola/controllers/default.py",line 94,in gQLquery2
response = fetch(url,headers )
File "D:\Oscar\_Particular\web2py\gluon\tools.py",line 4642,in fetch
html = urlopen(req).read()
File "urllib\request.py",line 222,in urlopen
File "urllib\request.py",line 523,in open
File "urllib\request.py",line 1247,in do_request_
TypeError: POST data should be bytes,an iterable of bytes,or a file object. It cannot be of type str.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)