suhkha
suhkha

Reputation: 581

Failed to decode JSON object: Expecting value: line 1 column 1 (char 0) - Beginner

I got this little code but when I run the endpoint in postman I got the following error:

Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)

I'm new with this language so I don't know what's going on.. ?

# -*- coding: utf-8 -*-

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/api/name', methods=['POST'])

def keyword_list():
  data = request.get_json(force=True)
  return jsonify({'data': data})

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

Thanks!

Upvotes: 1

Views: 3927

Answers (1)

Jim
Jim

Reputation: 73936

data = request.get_json(force=True)

Here you attempt to take what was sent from the HTTP client to your server and parse it as JSON. The JSON parser is rejecting it, saying that it immediately failed to parse it as JSON right at the very first character.

It's likely that what you have sent to the server is not valid JSON. You can see what you are sending to the server by using your browser's developer tools (if you are using a browser), or by logging request.data and looking at what you are receiving in your server logs.

Upvotes: 4

Related Questions