Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request
Asked Answered
D

2

7

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.

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.

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 (Running on Hasura cluster)

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.

Denier answered 26/1, 2018 at 14:52 Comment(7)
Not related to your problem, but if your request is a failure you shouldn't return 200.Inevasible
@DanielRoseman Okay! Will make note of that.Denier
My guess: you are getting a redirect.Try the requests.post with allow_redirects=FalseAngiology
@Angiology Yeah, I did that now, and now it reads '301 Moved Permanently'. Expected?Denier
@JoelKingsley As I said, I was only guessing from symptoms, that there might by a post/redirect/get pattern hidden somewhere.Angiology
@JoelKingsley Did you try using the https URL instead of the http one? I visited the URL that you linked to in your test.py file, and it's an https URL now. Maybe try that and run test.py?Reiterate
@Reiterate Wow! Just had to use https instead of http. Works now.Denier
R
3

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

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

Reiterate answered 30/1, 2018 at 10:11 Comment(0)
A
13

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)

2) You look for created_on and modified_on in your Flask app, but you send created-on and modified-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)
Abeabeam answered 26/1, 2018 at 16:9 Comment(2)
Forgot to mention, but some of the imports threw errors as wellAbeabeam
Found this useful to get rid of those 2 issues. But the main issue still exists. Might be a cluster service-specific problem, as it works for me locally too.Denier
R
3

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

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

Reiterate answered 30/1, 2018 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.