Drivehandler: Modify: 是否可以保持 `GET` 请求与默认相同,仅修改 `POST`

huangapple go评论80阅读模式
英文:

Drivehandler: Modify: Is it possible to keep the `GET` request same as default, but only modify the `POST `

问题

保持帮助文档中的默认配置,我们有

url:
  drivehandler:
    pattern: /$YAMLURL/drive
    handler: DriveHandler
    kwargs:
      path: $YAMLURL/drive
      modify: mymodule.modify(handler)

mymodule.py 被创建为:

def modify():
    if handler.request.method.upper() == "POST":
        import pandas as pd
        data = []
        for d in pd.DataFrame(handler.files).itertuples():
            print("预处理的数据", d)

Gramex 服务器在默认端口运行,POST 运行良好,在浏览器控制台中我们得到打印语句的输出。
然而,当我们尝试 GET localhost:9988/drive 时;虽然 drive/.meta.db 已填充,但结果始终为空。
所以想知道如果我修改了 POST 请求,我是否也需要创建一个自定义修改来处理 GET 请求?

英文:

Keeping default config from help document we have

url:
  drivehandler:
    pattern: /$YAMLURL/drive
    handler: DriveHandler
    kwargs:
      path: $YAMLURL/drive
      modify: mymodule.modify(handler)

mymodule.py is created as:

def modify():
    if handler.request.method.upper() == "POST":
        import pandas as pd
        data = []
        for d in pd.DataFrame(handler.files).itertuples():
            print("preprocesed data", d)

And gramex server is ran in the default port,
POST works great and on the browser console we get the output of the print statement.
However, when we try to GET localhost:9988/drive; Although the drive/.meta.db is populated, the result is always blank.
So wondering if I modify the POST request, Do I need to create a custom modification to GET as well?

答案1

得分: 0

For a GET method, this function returns None. Instead, you could use:

def modify(handler, data):
    if handler.request.method.upper() == "POST":
        import pandas as pd

        data = []
        for d in pd.DataFrame(handler.files).itertuples():
            print("preprocessed data", d)
    else:
        return data

You would also need to modify gramex.yaml to reflect the data argument:

      modify: mymodule.modify(handler, data)
英文:

For a GET method, this function returns None. Instead, you could use:

def modify(handler, data):
    if handler.request.method.upper() == "POST":
        import pandas as pd

        data = []
        for d in pd.DataFrame(handler.files).itertuples():
            print("preprocesed data", d)
    else:
        return data

You would also need to modify gramex.yaml to reflect the data argument:

      modify: mymodule.modify(handler, data)

huangapple
  • 本文由 发表于 2023年7月4日 21:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613147.html
  • gramex
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定