Get request body as string in Django

前端 未结 3 1649
旧时难觅i
旧时难觅i 2020-12-01 17:45

I\'m sending a POST request with JSON body to a Django server (fairly standard). On the server I need to decode this using json.loads().

The problem is

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 18:39

    If your goal is to end up with a dictionary of the data you have just sent to the server using JSON, save yourself the trouble of decoding the body yourself and use the request.POST dictionary-like object django already provides out-of-the-box.

    So suppose you POST this to the server:

    { 'foo': 'bar' }
    

    Then the following method

    def my_handler(request):
        foo = request.POST['foo']
        print(foo)
    

    Would print bar to the console

提交回复
热议问题