0

I have 1 small program. I'm sending the request of json via CURL X POST and should get the answer. The client code is:

curl -XPOST -d'{"animal":"cow", "sound":"moooo", "count": "3"}' 192.168.88.88:5000/test

The server code is:

import json
from flask import Flask, request, jsonify

app = Flask(__name)

@app.route("/test", methods=['POST'])
def json_example():
  content = request.get_json(silent=True)
  animal = content["animal"]
  sound = content["sound"]
  count = str(content["count"])
  i=0
  res=''
  while i<int(count):
    temp = animal + "says " + sound +"\n"
    res = temp +res
    i = i + 1
  return res

app.run(host='0.0.0.0', port = 5000)

If I'm running the request at postman and indicating that this is json type - then all is OK, but if I'm running it via curl - has an error (None type object isn't subscriptable) Please advice, how to correct the code at server and indicate, that all responses will be type of json.

Thanks

ShivaGaire
  • 2,283
  • 1
  • 20
  • 31

1 Answers1

1

You have to specify -H "Content-Type: application/json" with the curl request. Alternatively you can use request.form.values if you don't specify header type for the request:

curl -H "Content-Type: application/json"  -XPOST -d'{"animal":"cow", "sound":"moooo", "count": "3"}' 192.168.88.88:5000/test

For your curl request the mimetype given by request.mimetype is 'application/x-www-form-urlencoded'

ShivaGaire
  • 2,283
  • 1
  • 20
  • 31