How to get POSTed json in Flask?











up vote
192
down vote

favorite
37












I'm trying to build a simple API using Flask, in which I now want to read some POSTed JSON. I do the post with the PostMan Chrome extension, and the JSON I post is simply {"text":"lalala"}. I try to read the JSON using the following method:



@app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
def add_message(uuid):
content = request.json
print content
return uuid


On the browser it correctly returns the uuid I put in the GET, but on the console, it just prints out None (where I expect it to print out the {"text":"lalala"}. Does anybody know how I can get the posted JSON from within the Flask method?










share|improve this question




























    up vote
    192
    down vote

    favorite
    37












    I'm trying to build a simple API using Flask, in which I now want to read some POSTed JSON. I do the post with the PostMan Chrome extension, and the JSON I post is simply {"text":"lalala"}. I try to read the JSON using the following method:



    @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
    def add_message(uuid):
    content = request.json
    print content
    return uuid


    On the browser it correctly returns the uuid I put in the GET, but on the console, it just prints out None (where I expect it to print out the {"text":"lalala"}. Does anybody know how I can get the posted JSON from within the Flask method?










    share|improve this question


























      up vote
      192
      down vote

      favorite
      37









      up vote
      192
      down vote

      favorite
      37






      37





      I'm trying to build a simple API using Flask, in which I now want to read some POSTed JSON. I do the post with the PostMan Chrome extension, and the JSON I post is simply {"text":"lalala"}. I try to read the JSON using the following method:



      @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
      def add_message(uuid):
      content = request.json
      print content
      return uuid


      On the browser it correctly returns the uuid I put in the GET, but on the console, it just prints out None (where I expect it to print out the {"text":"lalala"}. Does anybody know how I can get the posted JSON from within the Flask method?










      share|improve this question















      I'm trying to build a simple API using Flask, in which I now want to read some POSTed JSON. I do the post with the PostMan Chrome extension, and the JSON I post is simply {"text":"lalala"}. I try to read the JSON using the following method:



      @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
      def add_message(uuid):
      content = request.json
      print content
      return uuid


      On the browser it correctly returns the uuid I put in the GET, but on the console, it just prints out None (where I expect it to print out the {"text":"lalala"}. Does anybody know how I can get the posted JSON from within the Flask method?







      python json post flask






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 3 '15 at 14:26









      Martijn Pieters

      688k12623682220




      688k12623682220










      asked Nov 15 '13 at 12:35









      kramer65

      8,86055171306




      8,86055171306
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          273
          down vote



          accepted










          You need to set the request content type to application/json for the .json property to work; it'll be None otherwise. See the Flask Request documentation:




          If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be None.




          Flask 0.10 added the request.get_json() method, and you should use that method instead of the .json property. You can tell the method to skip the content type requirement by setting force=True.



          Note that if an exception is raised at this point (possibly resulting in a 400 Bad Request response), your JSON data is invalid. It is in some way malformed; you may want to check it with a JSON validator.






          share|improve this answer



















          • 2




            Alright. And would you have any idea how to do that?
            – kramer65
            Nov 15 '13 at 12:49






          • 7




            @kramer65: How are you posting the request now? The client has to set the header; if you are using requests, that'd be request.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps({'text': 'lalala'}).
            – Martijn Pieters
            Nov 15 '13 at 12:52








          • 1




            Ah, now I understand. I had to set it at the sending party (i.e.: in PostMan). Okay, so I set that to json, but request.json is still a NoneType. ANy idea what else I could do wrong?
            – kramer65
            Nov 15 '13 at 12:57






          • 4




            @kramer65: See the source code; Either the Content-Type header is wrong, or the JSON you sent was 'null', which translates to None. Everything else raises an exception or returns your dictionary. Try request.get_json(force=True); this will ignore the header requirement.
            – Martijn Pieters
            Nov 15 '13 at 13:00












          • Okay! "I" fixed it! Using get_json(force=True) fixed it, but you were right all along; I set the content type in PostMan to JSON, but I didn't specifically set a header. So I now set the header and it all works fine. Thanks for the awesome help!
            – kramer65
            Nov 15 '13 at 13:04


















          up vote
          47
          down vote













          For reference, here's complete code for how to send json from a Python client:



          import requests
          res = requests.post('http://localhost:5000/api/add_message/1234', json={"mytext":"lalala"})
          if res.ok:
          print res.json()


          The "json=" input will automatically set the content-type, as discussed here: Post JSON using Python Requests



          And the above client will work with this server-side code:



          from flask import Flask, request, jsonify
          app = Flask(__name__)

          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.json
          print content['mytext']
          return jsonify({"uuid":uuid})

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





          share|improve this answer























          • This actually didn't work for me. With python 2.7 if I specify the <uuid> argument the request gets denied with a 404. This is when sending a valid JSON POST with both Postman and a ReactJS application. If I omit the arg it works just fine.
            – Omortis
            Feb 23 '17 at 20:28








          • 2




            This example definitely works with Python 2.7. Make sure you actually have the "<" and ">" in the app.route. The left/right carets are part of the required Flask syntax.
            – Luke
            Feb 24 '17 at 2:08






          • 1




            Yes, it does work. I was burying my request details in the JSON payload, not in the URI (as in: no arg supplied). Sorry for the static!
            – Omortis
            Feb 27 '17 at 13:01


















          up vote
          43
          down vote













          This is the way I would do it and it should be



          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.get_json(silent=True)
          print content
          return uuid


          With silent=True set, the get_json function will fail silently when trying to retrieve the json body. By default this is set to False. Setting force=True will ignore the request.headers.get('Content-Type') == 'application/json' check that flask does for you. By default this is also set to False. See flask documentation.



          I would strongly recommend leaving force=False and make the client send the Content-Type header to make it more explicit.



          Hope this helps!






          share|improve this answer

















          • 4




            Isn't it better to fail on errors??
            – vidstige
            Oct 4 '16 at 9:02










          • Depends if the json body is optional or not, so depends on your case
            – radtek
            Oct 5 '16 at 18:37






          • 1




            I cannot see any case where it would make sense to some times post valid json and other times invalid json. Sounds like two different end points
            – vidstige
            Oct 5 '16 at 18:39










          • Like I said, if an endpoint takes "optional" json body, you can use silent=True. Yes this is possible, and I do use it. Its really based on how you design your API to be consumed. If there is no case like that for your endpoint, just remove silent=True or explicitly set it to False.
            – radtek
            Oct 5 '16 at 18:57




















          up vote
          6
          down vote













          This solution works:



          from flask import Flask, request, jsonify

          app = Flask(__name__)


          @app.route('/hello', methods=['POST'])
          def hello():
          return jsonify(request.json)





          share|improve this answer

















          • 3




            To add to this answer the request you could send to this endpoint could be response = request.post('http://127.0.0.1:5000/hello', json={"foo": "bar"}). Following this running response.json() should return {'foo': 'bar'}
            – ScottMcC
            Jun 11 '17 at 8:54













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20001229%2fhow-to-get-posted-json-in-flask%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          273
          down vote



          accepted










          You need to set the request content type to application/json for the .json property to work; it'll be None otherwise. See the Flask Request documentation:




          If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be None.




          Flask 0.10 added the request.get_json() method, and you should use that method instead of the .json property. You can tell the method to skip the content type requirement by setting force=True.



          Note that if an exception is raised at this point (possibly resulting in a 400 Bad Request response), your JSON data is invalid. It is in some way malformed; you may want to check it with a JSON validator.






          share|improve this answer



















          • 2




            Alright. And would you have any idea how to do that?
            – kramer65
            Nov 15 '13 at 12:49






          • 7




            @kramer65: How are you posting the request now? The client has to set the header; if you are using requests, that'd be request.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps({'text': 'lalala'}).
            – Martijn Pieters
            Nov 15 '13 at 12:52








          • 1




            Ah, now I understand. I had to set it at the sending party (i.e.: in PostMan). Okay, so I set that to json, but request.json is still a NoneType. ANy idea what else I could do wrong?
            – kramer65
            Nov 15 '13 at 12:57






          • 4




            @kramer65: See the source code; Either the Content-Type header is wrong, or the JSON you sent was 'null', which translates to None. Everything else raises an exception or returns your dictionary. Try request.get_json(force=True); this will ignore the header requirement.
            – Martijn Pieters
            Nov 15 '13 at 13:00












          • Okay! "I" fixed it! Using get_json(force=True) fixed it, but you were right all along; I set the content type in PostMan to JSON, but I didn't specifically set a header. So I now set the header and it all works fine. Thanks for the awesome help!
            – kramer65
            Nov 15 '13 at 13:04















          up vote
          273
          down vote



          accepted










          You need to set the request content type to application/json for the .json property to work; it'll be None otherwise. See the Flask Request documentation:




          If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be None.




          Flask 0.10 added the request.get_json() method, and you should use that method instead of the .json property. You can tell the method to skip the content type requirement by setting force=True.



          Note that if an exception is raised at this point (possibly resulting in a 400 Bad Request response), your JSON data is invalid. It is in some way malformed; you may want to check it with a JSON validator.






          share|improve this answer



















          • 2




            Alright. And would you have any idea how to do that?
            – kramer65
            Nov 15 '13 at 12:49






          • 7




            @kramer65: How are you posting the request now? The client has to set the header; if you are using requests, that'd be request.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps({'text': 'lalala'}).
            – Martijn Pieters
            Nov 15 '13 at 12:52








          • 1




            Ah, now I understand. I had to set it at the sending party (i.e.: in PostMan). Okay, so I set that to json, but request.json is still a NoneType. ANy idea what else I could do wrong?
            – kramer65
            Nov 15 '13 at 12:57






          • 4




            @kramer65: See the source code; Either the Content-Type header is wrong, or the JSON you sent was 'null', which translates to None. Everything else raises an exception or returns your dictionary. Try request.get_json(force=True); this will ignore the header requirement.
            – Martijn Pieters
            Nov 15 '13 at 13:00












          • Okay! "I" fixed it! Using get_json(force=True) fixed it, but you were right all along; I set the content type in PostMan to JSON, but I didn't specifically set a header. So I now set the header and it all works fine. Thanks for the awesome help!
            – kramer65
            Nov 15 '13 at 13:04













          up vote
          273
          down vote



          accepted







          up vote
          273
          down vote



          accepted






          You need to set the request content type to application/json for the .json property to work; it'll be None otherwise. See the Flask Request documentation:




          If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be None.




          Flask 0.10 added the request.get_json() method, and you should use that method instead of the .json property. You can tell the method to skip the content type requirement by setting force=True.



          Note that if an exception is raised at this point (possibly resulting in a 400 Bad Request response), your JSON data is invalid. It is in some way malformed; you may want to check it with a JSON validator.






          share|improve this answer














          You need to set the request content type to application/json for the .json property to work; it'll be None otherwise. See the Flask Request documentation:




          If the mimetype is application/json this will contain the parsed JSON data. Otherwise this will be None.




          Flask 0.10 added the request.get_json() method, and you should use that method instead of the .json property. You can tell the method to skip the content type requirement by setting force=True.



          Note that if an exception is raised at this point (possibly resulting in a 400 Bad Request response), your JSON data is invalid. It is in some way malformed; you may want to check it with a JSON validator.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 5 '17 at 12:25

























          answered Nov 15 '13 at 12:38









          Martijn Pieters

          688k12623682220




          688k12623682220








          • 2




            Alright. And would you have any idea how to do that?
            – kramer65
            Nov 15 '13 at 12:49






          • 7




            @kramer65: How are you posting the request now? The client has to set the header; if you are using requests, that'd be request.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps({'text': 'lalala'}).
            – Martijn Pieters
            Nov 15 '13 at 12:52








          • 1




            Ah, now I understand. I had to set it at the sending party (i.e.: in PostMan). Okay, so I set that to json, but request.json is still a NoneType. ANy idea what else I could do wrong?
            – kramer65
            Nov 15 '13 at 12:57






          • 4




            @kramer65: See the source code; Either the Content-Type header is wrong, or the JSON you sent was 'null', which translates to None. Everything else raises an exception or returns your dictionary. Try request.get_json(force=True); this will ignore the header requirement.
            – Martijn Pieters
            Nov 15 '13 at 13:00












          • Okay! "I" fixed it! Using get_json(force=True) fixed it, but you were right all along; I set the content type in PostMan to JSON, but I didn't specifically set a header. So I now set the header and it all works fine. Thanks for the awesome help!
            – kramer65
            Nov 15 '13 at 13:04














          • 2




            Alright. And would you have any idea how to do that?
            – kramer65
            Nov 15 '13 at 12:49






          • 7




            @kramer65: How are you posting the request now? The client has to set the header; if you are using requests, that'd be request.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps({'text': 'lalala'}).
            – Martijn Pieters
            Nov 15 '13 at 12:52








          • 1




            Ah, now I understand. I had to set it at the sending party (i.e.: in PostMan). Okay, so I set that to json, but request.json is still a NoneType. ANy idea what else I could do wrong?
            – kramer65
            Nov 15 '13 at 12:57






          • 4




            @kramer65: See the source code; Either the Content-Type header is wrong, or the JSON you sent was 'null', which translates to None. Everything else raises an exception or returns your dictionary. Try request.get_json(force=True); this will ignore the header requirement.
            – Martijn Pieters
            Nov 15 '13 at 13:00












          • Okay! "I" fixed it! Using get_json(force=True) fixed it, but you were right all along; I set the content type in PostMan to JSON, but I didn't specifically set a header. So I now set the header and it all works fine. Thanks for the awesome help!
            – kramer65
            Nov 15 '13 at 13:04








          2




          2




          Alright. And would you have any idea how to do that?
          – kramer65
          Nov 15 '13 at 12:49




          Alright. And would you have any idea how to do that?
          – kramer65
          Nov 15 '13 at 12:49




          7




          7




          @kramer65: How are you posting the request now? The client has to set the header; if you are using requests, that'd be request.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps({'text': 'lalala'}).
          – Martijn Pieters
          Nov 15 '13 at 12:52






          @kramer65: How are you posting the request now? The client has to set the header; if you are using requests, that'd be request.post(url, headers={'Content-Type': 'application/json'}, data=json.dumps({'text': 'lalala'}).
          – Martijn Pieters
          Nov 15 '13 at 12:52






          1




          1




          Ah, now I understand. I had to set it at the sending party (i.e.: in PostMan). Okay, so I set that to json, but request.json is still a NoneType. ANy idea what else I could do wrong?
          – kramer65
          Nov 15 '13 at 12:57




          Ah, now I understand. I had to set it at the sending party (i.e.: in PostMan). Okay, so I set that to json, but request.json is still a NoneType. ANy idea what else I could do wrong?
          – kramer65
          Nov 15 '13 at 12:57




          4




          4




          @kramer65: See the source code; Either the Content-Type header is wrong, or the JSON you sent was 'null', which translates to None. Everything else raises an exception or returns your dictionary. Try request.get_json(force=True); this will ignore the header requirement.
          – Martijn Pieters
          Nov 15 '13 at 13:00






          @kramer65: See the source code; Either the Content-Type header is wrong, or the JSON you sent was 'null', which translates to None. Everything else raises an exception or returns your dictionary. Try request.get_json(force=True); this will ignore the header requirement.
          – Martijn Pieters
          Nov 15 '13 at 13:00














          Okay! "I" fixed it! Using get_json(force=True) fixed it, but you were right all along; I set the content type in PostMan to JSON, but I didn't specifically set a header. So I now set the header and it all works fine. Thanks for the awesome help!
          – kramer65
          Nov 15 '13 at 13:04




          Okay! "I" fixed it! Using get_json(force=True) fixed it, but you were right all along; I set the content type in PostMan to JSON, but I didn't specifically set a header. So I now set the header and it all works fine. Thanks for the awesome help!
          – kramer65
          Nov 15 '13 at 13:04












          up vote
          47
          down vote













          For reference, here's complete code for how to send json from a Python client:



          import requests
          res = requests.post('http://localhost:5000/api/add_message/1234', json={"mytext":"lalala"})
          if res.ok:
          print res.json()


          The "json=" input will automatically set the content-type, as discussed here: Post JSON using Python Requests



          And the above client will work with this server-side code:



          from flask import Flask, request, jsonify
          app = Flask(__name__)

          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.json
          print content['mytext']
          return jsonify({"uuid":uuid})

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





          share|improve this answer























          • This actually didn't work for me. With python 2.7 if I specify the <uuid> argument the request gets denied with a 404. This is when sending a valid JSON POST with both Postman and a ReactJS application. If I omit the arg it works just fine.
            – Omortis
            Feb 23 '17 at 20:28








          • 2




            This example definitely works with Python 2.7. Make sure you actually have the "<" and ">" in the app.route. The left/right carets are part of the required Flask syntax.
            – Luke
            Feb 24 '17 at 2:08






          • 1




            Yes, it does work. I was burying my request details in the JSON payload, not in the URI (as in: no arg supplied). Sorry for the static!
            – Omortis
            Feb 27 '17 at 13:01















          up vote
          47
          down vote













          For reference, here's complete code for how to send json from a Python client:



          import requests
          res = requests.post('http://localhost:5000/api/add_message/1234', json={"mytext":"lalala"})
          if res.ok:
          print res.json()


          The "json=" input will automatically set the content-type, as discussed here: Post JSON using Python Requests



          And the above client will work with this server-side code:



          from flask import Flask, request, jsonify
          app = Flask(__name__)

          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.json
          print content['mytext']
          return jsonify({"uuid":uuid})

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





          share|improve this answer























          • This actually didn't work for me. With python 2.7 if I specify the <uuid> argument the request gets denied with a 404. This is when sending a valid JSON POST with both Postman and a ReactJS application. If I omit the arg it works just fine.
            – Omortis
            Feb 23 '17 at 20:28








          • 2




            This example definitely works with Python 2.7. Make sure you actually have the "<" and ">" in the app.route. The left/right carets are part of the required Flask syntax.
            – Luke
            Feb 24 '17 at 2:08






          • 1




            Yes, it does work. I was burying my request details in the JSON payload, not in the URI (as in: no arg supplied). Sorry for the static!
            – Omortis
            Feb 27 '17 at 13:01













          up vote
          47
          down vote










          up vote
          47
          down vote









          For reference, here's complete code for how to send json from a Python client:



          import requests
          res = requests.post('http://localhost:5000/api/add_message/1234', json={"mytext":"lalala"})
          if res.ok:
          print res.json()


          The "json=" input will automatically set the content-type, as discussed here: Post JSON using Python Requests



          And the above client will work with this server-side code:



          from flask import Flask, request, jsonify
          app = Flask(__name__)

          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.json
          print content['mytext']
          return jsonify({"uuid":uuid})

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





          share|improve this answer














          For reference, here's complete code for how to send json from a Python client:



          import requests
          res = requests.post('http://localhost:5000/api/add_message/1234', json={"mytext":"lalala"})
          if res.ok:
          print res.json()


          The "json=" input will automatically set the content-type, as discussed here: Post JSON using Python Requests



          And the above client will work with this server-side code:



          from flask import Flask, request, jsonify
          app = Flask(__name__)

          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.json
          print content['mytext']
          return jsonify({"uuid":uuid})

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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 23 '17 at 12:10









          Community

          11




          11










          answered Feb 24 '16 at 22:15









          Luke

          2,0031521




          2,0031521












          • This actually didn't work for me. With python 2.7 if I specify the <uuid> argument the request gets denied with a 404. This is when sending a valid JSON POST with both Postman and a ReactJS application. If I omit the arg it works just fine.
            – Omortis
            Feb 23 '17 at 20:28








          • 2




            This example definitely works with Python 2.7. Make sure you actually have the "<" and ">" in the app.route. The left/right carets are part of the required Flask syntax.
            – Luke
            Feb 24 '17 at 2:08






          • 1




            Yes, it does work. I was burying my request details in the JSON payload, not in the URI (as in: no arg supplied). Sorry for the static!
            – Omortis
            Feb 27 '17 at 13:01


















          • This actually didn't work for me. With python 2.7 if I specify the <uuid> argument the request gets denied with a 404. This is when sending a valid JSON POST with both Postman and a ReactJS application. If I omit the arg it works just fine.
            – Omortis
            Feb 23 '17 at 20:28








          • 2




            This example definitely works with Python 2.7. Make sure you actually have the "<" and ">" in the app.route. The left/right carets are part of the required Flask syntax.
            – Luke
            Feb 24 '17 at 2:08






          • 1




            Yes, it does work. I was burying my request details in the JSON payload, not in the URI (as in: no arg supplied). Sorry for the static!
            – Omortis
            Feb 27 '17 at 13:01
















          This actually didn't work for me. With python 2.7 if I specify the <uuid> argument the request gets denied with a 404. This is when sending a valid JSON POST with both Postman and a ReactJS application. If I omit the arg it works just fine.
          – Omortis
          Feb 23 '17 at 20:28






          This actually didn't work for me. With python 2.7 if I specify the <uuid> argument the request gets denied with a 404. This is when sending a valid JSON POST with both Postman and a ReactJS application. If I omit the arg it works just fine.
          – Omortis
          Feb 23 '17 at 20:28






          2




          2




          This example definitely works with Python 2.7. Make sure you actually have the "<" and ">" in the app.route. The left/right carets are part of the required Flask syntax.
          – Luke
          Feb 24 '17 at 2:08




          This example definitely works with Python 2.7. Make sure you actually have the "<" and ">" in the app.route. The left/right carets are part of the required Flask syntax.
          – Luke
          Feb 24 '17 at 2:08




          1




          1




          Yes, it does work. I was burying my request details in the JSON payload, not in the URI (as in: no arg supplied). Sorry for the static!
          – Omortis
          Feb 27 '17 at 13:01




          Yes, it does work. I was burying my request details in the JSON payload, not in the URI (as in: no arg supplied). Sorry for the static!
          – Omortis
          Feb 27 '17 at 13:01










          up vote
          43
          down vote













          This is the way I would do it and it should be



          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.get_json(silent=True)
          print content
          return uuid


          With silent=True set, the get_json function will fail silently when trying to retrieve the json body. By default this is set to False. Setting force=True will ignore the request.headers.get('Content-Type') == 'application/json' check that flask does for you. By default this is also set to False. See flask documentation.



          I would strongly recommend leaving force=False and make the client send the Content-Type header to make it more explicit.



          Hope this helps!






          share|improve this answer

















          • 4




            Isn't it better to fail on errors??
            – vidstige
            Oct 4 '16 at 9:02










          • Depends if the json body is optional or not, so depends on your case
            – radtek
            Oct 5 '16 at 18:37






          • 1




            I cannot see any case where it would make sense to some times post valid json and other times invalid json. Sounds like two different end points
            – vidstige
            Oct 5 '16 at 18:39










          • Like I said, if an endpoint takes "optional" json body, you can use silent=True. Yes this is possible, and I do use it. Its really based on how you design your API to be consumed. If there is no case like that for your endpoint, just remove silent=True or explicitly set it to False.
            – radtek
            Oct 5 '16 at 18:57

















          up vote
          43
          down vote













          This is the way I would do it and it should be



          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.get_json(silent=True)
          print content
          return uuid


          With silent=True set, the get_json function will fail silently when trying to retrieve the json body. By default this is set to False. Setting force=True will ignore the request.headers.get('Content-Type') == 'application/json' check that flask does for you. By default this is also set to False. See flask documentation.



          I would strongly recommend leaving force=False and make the client send the Content-Type header to make it more explicit.



          Hope this helps!






          share|improve this answer

















          • 4




            Isn't it better to fail on errors??
            – vidstige
            Oct 4 '16 at 9:02










          • Depends if the json body is optional or not, so depends on your case
            – radtek
            Oct 5 '16 at 18:37






          • 1




            I cannot see any case where it would make sense to some times post valid json and other times invalid json. Sounds like two different end points
            – vidstige
            Oct 5 '16 at 18:39










          • Like I said, if an endpoint takes "optional" json body, you can use silent=True. Yes this is possible, and I do use it. Its really based on how you design your API to be consumed. If there is no case like that for your endpoint, just remove silent=True or explicitly set it to False.
            – radtek
            Oct 5 '16 at 18:57















          up vote
          43
          down vote










          up vote
          43
          down vote









          This is the way I would do it and it should be



          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.get_json(silent=True)
          print content
          return uuid


          With silent=True set, the get_json function will fail silently when trying to retrieve the json body. By default this is set to False. Setting force=True will ignore the request.headers.get('Content-Type') == 'application/json' check that flask does for you. By default this is also set to False. See flask documentation.



          I would strongly recommend leaving force=False and make the client send the Content-Type header to make it more explicit.



          Hope this helps!






          share|improve this answer












          This is the way I would do it and it should be



          @app.route('/api/add_message/<uuid>', methods=['GET', 'POST'])
          def add_message(uuid):
          content = request.get_json(silent=True)
          print content
          return uuid


          With silent=True set, the get_json function will fail silently when trying to retrieve the json body. By default this is set to False. Setting force=True will ignore the request.headers.get('Content-Type') == 'application/json' check that flask does for you. By default this is also set to False. See flask documentation.



          I would strongly recommend leaving force=False and make the client send the Content-Type header to make it more explicit.



          Hope this helps!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 27 '15 at 22:11









          radtek

          14.5k68072




          14.5k68072








          • 4




            Isn't it better to fail on errors??
            – vidstige
            Oct 4 '16 at 9:02










          • Depends if the json body is optional or not, so depends on your case
            – radtek
            Oct 5 '16 at 18:37






          • 1




            I cannot see any case where it would make sense to some times post valid json and other times invalid json. Sounds like two different end points
            – vidstige
            Oct 5 '16 at 18:39










          • Like I said, if an endpoint takes "optional" json body, you can use silent=True. Yes this is possible, and I do use it. Its really based on how you design your API to be consumed. If there is no case like that for your endpoint, just remove silent=True or explicitly set it to False.
            – radtek
            Oct 5 '16 at 18:57
















          • 4




            Isn't it better to fail on errors??
            – vidstige
            Oct 4 '16 at 9:02










          • Depends if the json body is optional or not, so depends on your case
            – radtek
            Oct 5 '16 at 18:37






          • 1




            I cannot see any case where it would make sense to some times post valid json and other times invalid json. Sounds like two different end points
            – vidstige
            Oct 5 '16 at 18:39










          • Like I said, if an endpoint takes "optional" json body, you can use silent=True. Yes this is possible, and I do use it. Its really based on how you design your API to be consumed. If there is no case like that for your endpoint, just remove silent=True or explicitly set it to False.
            – radtek
            Oct 5 '16 at 18:57










          4




          4




          Isn't it better to fail on errors??
          – vidstige
          Oct 4 '16 at 9:02




          Isn't it better to fail on errors??
          – vidstige
          Oct 4 '16 at 9:02












          Depends if the json body is optional or not, so depends on your case
          – radtek
          Oct 5 '16 at 18:37




          Depends if the json body is optional or not, so depends on your case
          – radtek
          Oct 5 '16 at 18:37




          1




          1




          I cannot see any case where it would make sense to some times post valid json and other times invalid json. Sounds like two different end points
          – vidstige
          Oct 5 '16 at 18:39




          I cannot see any case where it would make sense to some times post valid json and other times invalid json. Sounds like two different end points
          – vidstige
          Oct 5 '16 at 18:39












          Like I said, if an endpoint takes "optional" json body, you can use silent=True. Yes this is possible, and I do use it. Its really based on how you design your API to be consumed. If there is no case like that for your endpoint, just remove silent=True or explicitly set it to False.
          – radtek
          Oct 5 '16 at 18:57






          Like I said, if an endpoint takes "optional" json body, you can use silent=True. Yes this is possible, and I do use it. Its really based on how you design your API to be consumed. If there is no case like that for your endpoint, just remove silent=True or explicitly set it to False.
          – radtek
          Oct 5 '16 at 18:57












          up vote
          6
          down vote













          This solution works:



          from flask import Flask, request, jsonify

          app = Flask(__name__)


          @app.route('/hello', methods=['POST'])
          def hello():
          return jsonify(request.json)





          share|improve this answer

















          • 3




            To add to this answer the request you could send to this endpoint could be response = request.post('http://127.0.0.1:5000/hello', json={"foo": "bar"}). Following this running response.json() should return {'foo': 'bar'}
            – ScottMcC
            Jun 11 '17 at 8:54

















          up vote
          6
          down vote













          This solution works:



          from flask import Flask, request, jsonify

          app = Flask(__name__)


          @app.route('/hello', methods=['POST'])
          def hello():
          return jsonify(request.json)





          share|improve this answer

















          • 3




            To add to this answer the request you could send to this endpoint could be response = request.post('http://127.0.0.1:5000/hello', json={"foo": "bar"}). Following this running response.json() should return {'foo': 'bar'}
            – ScottMcC
            Jun 11 '17 at 8:54















          up vote
          6
          down vote










          up vote
          6
          down vote









          This solution works:



          from flask import Flask, request, jsonify

          app = Flask(__name__)


          @app.route('/hello', methods=['POST'])
          def hello():
          return jsonify(request.json)





          share|improve this answer












          This solution works:



          from flask import Flask, request, jsonify

          app = Flask(__name__)


          @app.route('/hello', methods=['POST'])
          def hello():
          return jsonify(request.json)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 28 '17 at 15:40









          trojek

          620629




          620629








          • 3




            To add to this answer the request you could send to this endpoint could be response = request.post('http://127.0.0.1:5000/hello', json={"foo": "bar"}). Following this running response.json() should return {'foo': 'bar'}
            – ScottMcC
            Jun 11 '17 at 8:54
















          • 3




            To add to this answer the request you could send to this endpoint could be response = request.post('http://127.0.0.1:5000/hello', json={"foo": "bar"}). Following this running response.json() should return {'foo': 'bar'}
            – ScottMcC
            Jun 11 '17 at 8:54










          3




          3




          To add to this answer the request you could send to this endpoint could be response = request.post('http://127.0.0.1:5000/hello', json={"foo": "bar"}). Following this running response.json() should return {'foo': 'bar'}
          – ScottMcC
          Jun 11 '17 at 8:54






          To add to this answer the request you could send to this endpoint could be response = request.post('http://127.0.0.1:5000/hello', json={"foo": "bar"}). Following this running response.json() should return {'foo': 'bar'}
          – ScottMcC
          Jun 11 '17 at 8:54




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20001229%2fhow-to-get-posted-json-in-flask%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Xamarin.form Move up view when keyboard appear

          JBPM : POST request for execute process go wrong