向我的 RESTful API(Python-Flask)发送 POST 请求,但收到 GET 请求 [英] Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request

查看:47
本文介绍了向我的 RESTful API(Python-Flask)发送 POST 请求,但收到 GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以包含 JSON 的 POST 请求的形式向 Zapier Webhook 发送触发器.如果我只是通过本地 python 脚本发送 POST 请求,它工作正常.

I'm trying to send a trigger to a Zapier webhook in the form of a POST request containing JSON. It works fine if I just send the POST request through a local python script.

我想要做的是创建一个 RESTful API,它在调用 create-row-in-gs 端点时触发 Zapier webhook.

What I want to do is create a RESTful API which makes the trigger to the Zapier webhook when the create-row-in-gs endpoint is called.

如您所见,我正在向 Hasura 集群发送 POST 请求 API 调用.但是我得到的不是200 OK SUCCESS"的响应,而是200 OK failure",这意味着该请求被视为 GET 请求而不是 POST 请求.

As you can see, I'm sending a POST request API call to the Hasura cluster. But instead of getting the response as '200 OK SUCCESS', I'm getting a '200 OK failure' which means that the request is being treated as a GET request instead of a POST request.

test.py

#Python 3 Script to send a POST request containing JSON

import json
import requests

api_url = 'http://app.catercorner16.hasura-app.io/create-row-in-gs'
create_row_data = {'id': '1235','name':'Joel','created-on':'27/01/2018','modified-on':'27/01/2018','desc':'This is Joel!!'}
r = requests.post(url=api_url, data=create_row_data)
print(r.status_code, r.reason, r.text)

server.py(在 Hasura 集群上运行)

from src import app
from flask import jsonify,request,make_response,url_for,redirect
from json import dumps
from requests import post

url = 'https://hooks.zapier.com/hooks/catch/xxxxx/yyyyy/'

@app.route('/create-row-in-gs', methods=['GET','POST'])
def create_row_in_gs():
    if request.method == 'GET':
        return make_response('failure')
    if request.method == 'POST':
        t_id = request.json['id']
        t_name = request.json['name']
        created_on = request.json['created_on']
        modified_on = request.json['modified_on']
        desc = request.json['desc']

        create_row_data = {'id': str(t_id),'name':str(t_name),'created-on':str(created_on),'modified-on':str(modified_on),'desc':str(desc)}

        response = requests.post(
            url, data=json.dumps(create_row_data),
            headers={'Content-Type': 'application/json'}
        )
        return response

数周以来一直在为此苦苦挣扎.我究竟做错了什么?将不胜感激任何帮助.

Have been struggling with this for weeks. What am I doing wrong? Would appreciate any help.

推荐答案

确保您使用了正确的协议.httphttps.

Make sure you're using the right protocol. http or https.

如果您使用 http 并看到重定向,则重定向 Location 标头通常会具有正确的 URL.

If you use http and see a redirect, the redirect Location header will have the correct URL usually.

这篇关于向我的 RESTful API(Python-Flask)发送 POST 请求,但收到 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆