Home > OS >  collect others parameters in my flask api
collect others parameters in my flask api

Time:12-20

I send a json file by curl with this command and its work :

curl -X POST -H "Content-Type: application/json" -d @/Users/test/testjson.json 'http://127.0.0.1:5000

I collect this data like and its work :


@app.route('/json_test', methods=['POST'])
def process_data():
    # Collect the JSON file from the request
    data = json.loads(request.data)
    #
  

I want to add both parameters source and id.

How can I send these parameters in Curl plus my json file . I tried

curl -i -X POST -H "Content-Type: multipart/form-data" 
-F "data /Users/test/testjson.json” -F "source=Value1" -F "ident=Value2" http://127.0.0.1:5000

But it's not working

if you can help me to have the command and how i can read this data with python.

CodePudding user response:

Please provide more information of the content on both values you want to add.

I changed a quotation mark inside your curl, try with:

curl -i -X POST -H 'Content-Type: multipart/form-data' 
-F 'data /Users/test/testjson.json' -F "source=Value1" -F 'ident=Value2' http://127.0.0.1:5000

To use variables from the curl, you need to update your method. The below curl is another example you can try for now:

@app.route('/json_test', methods=['POST'])
def process_data(source: str, ident: str):
    # Collect the JSON file from the request
    data = json.loads(request.data)
    #

Curl Command:

curl -i -X POST \ 
  'http://127.0.0.1:5000/json_test/?source=value1/?ident=value2' \
  -H 'Content-Type: multipart/form-data' \
-F 'data /Users/test/testjson.json'

CodePudding user response:

Try this:

curl -i -X POST -H "Content-Type: multipart/form-data" 
-F "user_input=</Users/test/testjson.json” -F "source=Value1" -F "ident=Value2" http://127.0.0.1:5000/json_test

Note that this will create a form with three fields: user_input, source, and ident. You'll need to modify your controller endpoint as well:

@app.route('/json_test', methods=['POST'])
def process_data():
    form_content_as_json = json.loads(request.form)

    
  • Related