如何使用Python和Flask从Jotform接收形式的数据

发布于 2025-02-13 14:30:16 字数 247 浏览 0 评论 0原文

jotform 是在几分钟内轻松创建在线表单的绝佳服务。有时,您可能需要分析已发送的数据。 您可以要求Jotform在用户提交表单时通过Webhook向您发送通过Webhook收集的数据。问题在于,在Jotform文档中,只有PHP语言的示例。

另一方面,我需要用烧瓶中的Python获取这些数据...

Jotform is a great service to easily create online forms in minutes. Sometimes you may need to analyze the data that has been sent.
You can ask Jotform to send you the data collected via webhook whenever a user submits a form. The problem is that in the jotform documentation there are only examples for the PHP language.

On the other hand, I need to get that data in Python with Flask...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2025-02-20 14:30:16

我编写了这个小代码,以使用Python 3.6+和烧瓶接收和处理来自Jotform的数据。它将与大多数字段一起使用,我不确定文件和多媒体字段,因为我尚未对其进行测试。
如果它也可以帮助您,请随时使用它。

import json
from flask import Flask, request


app = Flask(__name__)


def extract_jotform_data():
    output = {}
    form_data = request.form.to_dict()
    if form_data.get("rawRequest"):
        for key, value in json.loads(form_data["rawRequest"]).items():
            # Removes the "q<number>_" part from the key name
            # Instead of "q5_quantity" we want "quantity" as the key
            temp = key.split("_")
            new_key = key if len(temp) == 1 else "_".join(temp[1:])
            # Saves the item with the new key in the dictionary
            output[new_key] = value

    return output


@app.route('/', methods=['GET', 'POST'])
def hello_world():
    jotform = extract_jotform_data()
    for key, value in jotform.items():
        print(f"{key}: {value}")
        if type(value) is dict:
            for subkey, subvalue in value.items():
                print(f" +------ {subkey}: {subvalue}")

    return "ok", 200


if __name__ == '__main__':
    app.run()

I wrote this little piece of code to receive and process the data from Jotform with Python 3.6+ and Flask. It will work with the majority of fields, I'm not sure about file and multimedia fields since I've not yet tested them.
Please feel free to use it, if it can help you too.

import json
from flask import Flask, request


app = Flask(__name__)


def extract_jotform_data():
    output = {}
    form_data = request.form.to_dict()
    if form_data.get("rawRequest"):
        for key, value in json.loads(form_data["rawRequest"]).items():
            # Removes the "q<number>_" part from the key name
            # Instead of "q5_quantity" we want "quantity" as the key
            temp = key.split("_")
            new_key = key if len(temp) == 1 else "_".join(temp[1:])
            # Saves the item with the new key in the dictionary
            output[new_key] = value

    return output


@app.route('/', methods=['GET', 'POST'])
def hello_world():
    jotform = extract_jotform_data()
    for key, value in jotform.items():
        print(f"{key}: {value}")
        if type(value) is dict:
            for subkey, subvalue in value.items():
                print(f" +------ {subkey}: {subvalue}")

    return "ok", 200


if __name__ == '__main__':
    app.run()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文