2

API looks like this

app = Flask(__name__)

@app.route('/mycalculator', methods = ['POST'])
def json_example():
    req_data = request.get_json()
    #..... did a bunch of computation 
    return response 

My API testing code look like:

import requests
import json

URL = "http://127.0.0.1:5000/mycalculator/"
def test_jsonget_guideline():
    file =  open('myjsonfile', 'r')
    json_input = file.read()    
    request_json = json.loads(json_input)
    print(request_json)

    # make post request within json input body
    response = requests.post(url=URL, 
                             data=request_json)                         
    if response.ok:
        print(response.content)
    else:
        print("response not okay. status code: {}".format(response.status_code))

I keep getting the error like this:

status code: 403

I was able to test this JSON file using Postman. But I want to write a python script to automate all the testings. Anyone know which step I am not doing right? I looked through many tutorial videos, but I was not able to fix this problem. I would greatly appreciate your help!

Elsa Li
  • 673
  • 3
  • 9
  • 19
  • Hello! May I know how you fixed the issue? I'm currently facing the same issue with requests.post(). I've tried using requests.post(url=URL, json=request_json) instead but I'm still facing the same error. Appreciate if you can share how you manage to fixed the issue! Thank you! :) – OinkOink Nov 20 '19 at 03:28
  • @OinkOink It turns out to be the proxy issue. Because my company network has firewalls... I just specified the proxy and then connection is okay. – Elsa Li Nov 23 '19 at 04:49

0 Answers0