Why am I not able to see the values that was posted in form

This is my code # import main Flask class and request object

from email.mime import application
from flask import Flask, request

# create the Flask app

# allow both GET and POST requests
@application.route('/', methods=['GET', 'POST'])
def form_example():
    # handle the POST request
    if request.method == 'POST':
        key = request.form.get('key')  # Retrieve the 'key' parameter
        name = request.form.get('name')  # Retrieve the 'name' parameter
        attachment = request.form.get('attachment')  # Retrieve the 'attachment' parameter
        description = request.form.get('description')  # Retrieve the 'description' parameter
        return '''
                  <h1>Key: {}</h1>
                  <h1>Name: {}</h1>
                  <h1>Attachment: {}</h1>
                  <h1>Description: {}</h1>
                '''.format(key, name, attachment, description)

    # otherwise handle the GET request
    return '''
           <form method="POST">
               <div><label>key: <input type="text" name="key"></label></div>
               <div><label>name: <input type="text" name="name"></label></div>
               <div><label>attachment: <input type="text" name="attachment"></label></div>
               <div><label>description: <input type="text" name="description"></label></div>
               <input type="submit" value="Submit">

if __name__ == '__main__':
    # run app in debug mode on port 5000

and I want to see the values that was posted in the form I tried using json but I was just seeing none. this was my json code

    "Key": "${Project}",
    "Name": "${Name}",
    "Description" : "${Description}"

Hey @lunar-module-sagani1 :wave: Welcome to Postman community!

I’ve spotted quite a few issues with the entire setup.

  • In your python code you are expecting form-data, while you are sending JSON payload from Postman
  • Postman encloses variables within two curly braces {{variable}}, you are incorrectly using ${Project}
  • You are using all lower case when fetching data in your python code, but the keys in use on Postman have the first letter capitalized: Key vs key

The easiest fix would be to update your Postman request to use form-data

Also make sure that you are setting some value inside the variables
I’ve setting some test data inside pre-request script as follows:

Here’s my quick test results using form-data and setting variable value in pre-request script, and the code you provided.

That works but how do I pull the values that was posted in the form like this one.

Thank you

Your API is outputting\returning HTML.

Therefore Postman is going to display that HTML including the tags, where your browser will show the formatted HTML.

If you want to pull the values properly from the response, then return it as JSON instead of HTML then have your front end consume the response and deal with the formatting.

Am I doing something wrong in my python code or in the postman?

@lunar-module-sagani1 Base on the latest screenshots you shared, you are still passing data as JSON via Postman and reading it as form data.

If that is what you want, check Flask docs to learn on how to read JSON data:

For consuming the response

I add a route to my code so now when they submit the form it will do a get method and hold under “/get_session_data”. But now in the postman what should I do to get that get values to display on the bottom and not get null as value.

The response showing as null is down to your code\API, not Postman.

I’m not a developer so I can’t help with Python\Flask code, but the same principle applies to Python for troubleshooting, which would be to console log (or the Python equivalent) your variables before you consume them.

Console log the response_data JSON variable in your Python code. Is it showing the values as you expect?

However, on face value, it still looks like you are trying to read the values as form data, when they are JSON, therefore I suspect those variables will be undefined or null.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.