9

I'm setting up a simple rest api using flask and flask-restful. Right now all I'm trying to do is create a post request with some Json data, and then return it just to see if it works. I always get the same error "message": "Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)"

Below is my code

from flask import Flask, jsonify, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)


class Tester(Resource):
   def get(self):
       return {'about': 'Hello World'}

   def post(self):
       data_json = request.get_json(force=True)
       return {'you sent': data_json}


api.add_resource(Tester, '/')

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

The curl request I am making to test this is below, I've also tried making request using postman

curl -H "Content-Type: application/json" -X POST -d '{'username':"abc",'password':"abc"}' http://localhost:5000
6
  • 2
    In postman, do you have raw-> JSON (application/json) option selected? Commented Feb 27, 2019 at 14:59
  • @needtobe No I did not. Just tried it and I got a 200 response. Thank you. That said why doesn't it seem to work in the curl request? Commented Feb 27, 2019 at 15:04
  • Generate the cURL request code using Postman Commented Feb 27, 2019 at 15:13
  • 1
    Try curl -i -H "Content-Type: application/json" -X POST -d "{\"username\":\"abc\", \"password\":\"abc\"}" 127.0.0.1:5000 instead. Commented Feb 27, 2019 at 15:24
  • 1
    @FProlog try mine cURL posted above. Commented Feb 27, 2019 at 15:27

4 Answers 4

13

You need to select raw -> JSON (application/json) in Postman like this:

enter image description here

When it comes to your cURL request, answer explains that windows's command line lacks support of strings with single quotes, so use:

curl -i -H "Content-Type: application/json" -X POST -d "{\"username\":\"abc\", \"password\":\"abc\"}" 127.0.0.1:5000

instead:

curl -H "Content-Type: application/json" -X POST -d '{'username':"abc",'password':"abc"}' http://localhost:5000

\ escapes " character.

Sign up to request clarification or add additional context in comments.

Comments

4

You also need to have the Content-Length header enabled.

Without header

With header

1 Comment

This was it for me, so strange!
2

Some additional information in case you are using python script to test your flask api like me - you have to dumps before adding the dictionary to the data field.

import requests
import json

response = requests.post(
    'http://localhost:5000', 
    data = json.dumps({'username':"abc",'password':"abc"}),
    headers = {"Content-Type": "application/json"}
    )

1 Comment

This answer solved my problem. Got rid of the JSON decode error and also converts datetime.datetime(2019, 4, 3, 9, 47, 9, 427000) to 2019-04-03 09:47:09.427000 on the fly. >>> json.dumps({'datetime':'datetime.datetime(2019, 4, 3, 9, 47, 9, 427000)'}) output: {'datetime':'2019-04-03 09:47:09.427000'}
0

The curl request (payload) is incorrect. Use double quotes in the payload.

curl -H "Content-Type: application/json" -X POST -d '{"username":"abc","password":"abc"}' http://localhost:5000

1 Comment

Single quote in your payload --> '{'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.