-1

I made an API using flask from which I can get and post data. I am able to get the data but am not able to post it.

Snippet of the code from which I am making a post request

import requests

from pprint import pprint

base_url = 'http://127.0.0.1:5000'

response = requests.get(base_url)

data = response.json()

#print(data)



print("Enter '1' If You Are Existing User")

print("Enter '2' If You Want To Make An Account")

key=int(input())

if(key==2):

    print("Enter Your Name For Your Account:")

    n1=input()
    print("Enter A Strong Password For Your Account")
    n2=input()
    for g in data:
        if(g['Name'].upper()== n1.upper() and g['Password'].upper()==n2.upper()):
           print("User Already Exist")
           break    
        else:
            payload={'Name':n1,'Password':n2}
            r=requests.post(base_url,data=payload)
            pprint(r.json)
if(key==1):

    print("Enter Your Name")
    name = input()
    print("Enter Your Password")
    ent = input()

    print("\n")

    for i in data:
        if(i['Name'].upper()== name.upper() and i['Password'].upper()==ent.upper()):
            print("Login Succesfull")

Snippet of the API I made to store data

Running The API which stores the data only gives me this result , it does not shows the data which i uploaded through Requests.post

1 Answers1

1

The error lies in calling requests.post with data=.... You should use json=... so it adds the header Content-Type: application/json automatically for you in the request.

Also, another error is that the request.post was inside the for-loop.

On the server-side, the error is the declaration of both GET and POST methods in the same function. A better way would be to use one function for each method.

Anyway, I've fixed the sources and the below code should work.


APP

import requests
from pprint import pprint

base_url = 'http://127.0.0.1:5000'
response = requests.get(base_url)
data = response.json()

#print(data)

print("Enter '1' If You Are Existing User")
print("Enter '2' If You Want To Make An Account")

key=int(input())

if(key==2):
    print("Enter Your Name For Your Account:")

    n1=input()
    print("Enter A Strong Password For Your Account")
    n2=input()
    for g in data:
        if(g['Name'].upper()== n1.upper() and g['Password'].upper()==n2.upper()):
           print("User Already Exist")
           break
    else:
        payload={'Name':n1,'Password':n2}
        r=requests.post(base_url,json=payload)
        pprint(r.text)

if(key==1):
    print("Enter Your Name")
    name = input()
    print("Enter Your Password")
    ent = input()

    print("\n")

    for i in data:
        if(i['Name'].upper()== name.upper() and i['Password'].upper()==ent.upper()):
            print("Login Succesfull")


API

from flask import Flask, jsonify, request

result = [
  {'Name': 'P1', 'Password': 'Youtube'},
  {'Name': 'R1', 'Password': 'hello'},
  {'Name': 'S1', 'Password': 'pubg'},
]

app = Flask(__name__)

@app.route('/', methods=['GET'])
def hello2():
  return jsonify(result)

@app.route('/', methods=['POST'])
def hello3():
  global result
  result += [request.json]
  return jsonify(result)

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

lepsch
  • 8,927
  • 5
  • 24
  • 44
  • But the data is still not posted to the API , i got the response as : [{'Name': 'P1', 'Password': 'Youtube'}, {'Name': 'R1', 'Password': 'hello'}, {'Name': 'S1', 'Password': 'pubg'}] [{'Name': 'P1', 'Password': 'Youtube'}, {'Name': 'R1', 'Password': 'hello'}, {'Name': 'S1', 'Password': 'pubg'}] [{'Name': 'P1', 'Password': 'Youtube'}, {'Name': 'R1', 'Password': 'hello'}, {'Name': 'S1', 'Password': 'pubg'}] – Rushil Pajni Jun 11 '22 at 22:35
  • Your API implementation has GET, POST and PUT answering exactly the same thing. If you want a different answer on the POST you should check which method is the request or create another `@app.route('/', methods=['POST'])`. I.e. one `@app.route` for each method only. – lepsch Jun 11 '22 at 23:06
  • @app.route('/', methods=['POST']) i implemented this but got HTTP 405 error , Method not allowed . So i tried @app.route('/',methods=['GET', 'POST']) as well , no error was found but still was unable to POST the data – Rushil Pajni Jun 11 '22 at 23:14
  • A 405 HTTP Status means you actually don't have declared the `POST` method anywhere. Maybe you've declared it `PUT` instead. – lepsch Jun 11 '22 at 23:38
  • where else should I have to declare the POST method in the code? – Rushil Pajni Jun 11 '22 at 23:44
  • @app.route('/', methods=['POST']) @app.route('/',methods=['GET']) i used this but no error came still i was'nt able to upload the data – Rushil Pajni Jun 11 '22 at 23:45
  • I've edited my answer with the fix. – lepsch Jun 12 '22 at 00:08