简体   繁体   English

向我的RESTful API(Python-Flask)发送POST请求,但收到GET请求

[英]Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request

I'm trying to send a trigger to a Zapier webhook in the form of a POST request containing JSON. 我正在尝试以包含JSON的POST请求的形式向Zapier webhook发送触发器。 It works fine if I just send the POST request through a local python script. 如果我只是通过本地python脚本发送POST请求,它工作正常。

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. 我想要做的是创建一个RESTful API,当调用create-row-in-gs端点时,它会触发Zapier webhook。

As you can see, I'm sending a POST request API call to the Hasura cluster. 如您所见,我正在向Hasura集群发送POST请求API调用。 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. 但是,我没有得到“200 OK SUCCESS”的响应,而是获得了“200 OK失败”,这意味着该请求被视为GET请求而不是POST请求。

test.py 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 (Running on Hasura cluster) 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. 非常感谢任何帮助。

Ok, I checked you script locally and found two issues. 好的,我在本地检查了你的脚本,发现了两个问题。 Both are in your client script. 两者都在您的客户端脚本中。

1) r = requests.post(url=api_url, data=create_row_data) should be r = requests.post(url=api_url, json=create_row_data) 1) r = requests.post(url=api_url, data=create_row_data)应为r = requests.post(url=api_url, json=create_row_data)

2) You look for created_on and modified_on in your Flask app, but you send created-on and modified-on . 2)您在Flask应用程序中查找created_onmodified_on ,但是您发送了created-onmodified-on

Working local code below: 使用以下本地代码:

Client: 客户:

import json
import requests

api_url = 'http://localhost:5000/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!!'}
print(create_row_data)
r = requests.post(url=api_url, json=create_row_data)
print(r.status_code, r.reason, r.text)

Server: 服务器:

from flask import Flask,jsonify,request,make_response,url_for,redirect
import requests, json

app = Flask(__name__)

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.content

if __name__ == '__main__':
    app.run(host='localhost',debug=False, use_reloader=True)

Make sure you're using the right protocol. 确保您使用的是正确的协议。 http or https . httphttps

If you use http and see a redirect, the redirect Location header will have the correct URL usually. 如果您使用http并查看重定向,则重定向Location标头通常会包含正确的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM