Django req.POST 总是 returns 错误
Django req.POST always returns False
我正在测试一个 shipstation webhook,我似乎无法从他们发送的 POST 请求中获取数据。
他们的 webhook docs 说他们的 POST 请求将包含如下所示的正文:
{"resource_url":"https://ssapiX.shipstation.com/orders?storeID=123456&importBatch=1ab23c4d-12ab-1abc-a1bc-a12b12cdabcd","resource_type":"ORDER_NOTIFY"}
为了调试这个问题,我进入了 Firefox 并尝试发送这个:
得到了同样的结果; req.method = 'POST'
和 req.POST = False
查看 myNgrokAddress.ngrok.io/bot/shipstation
的控制器:
@csrf_exempt
def vc(req):
print(req.META) //this works but it looks like meta-data for my browser and not from shipstation
print(req.POST.get('resource_url')) //prints false
print(req.POST) //prints false
return HttpResponse('')
当我转到 localhost:4040
(ngrok 检查器)时,POST 正文出现了,所以一定是我的 django 服务器上配置不正确。
我在 settings.py 中设置了 ALLOWED_HOSTS = ['myNgrokAdress.ngrok.io', 'localhost']
。我还需要做些什么吗?
我在这里错过了什么?
问题出在 req.POST
方法上。
来自Django docs :
HttpRequest.POST:
A dictionary-like object containing all given HTTP POST parameters, providing that the request contains form data. See the QueryDict documentation below. If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead.
由于发送的数据类型是非格式数据,您需要使用req.body
。
我正在测试一个 shipstation webhook,我似乎无法从他们发送的 POST 请求中获取数据。
他们的 webhook docs 说他们的 POST 请求将包含如下所示的正文:
{"resource_url":"https://ssapiX.shipstation.com/orders?storeID=123456&importBatch=1ab23c4d-12ab-1abc-a1bc-a12b12cdabcd","resource_type":"ORDER_NOTIFY"}
为了调试这个问题,我进入了 Firefox 并尝试发送这个:
得到了同样的结果; req.method = 'POST'
和 req.POST = False
查看 myNgrokAddress.ngrok.io/bot/shipstation
的控制器:
@csrf_exempt
def vc(req):
print(req.META) //this works but it looks like meta-data for my browser and not from shipstation
print(req.POST.get('resource_url')) //prints false
print(req.POST) //prints false
return HttpResponse('')
当我转到 localhost:4040
(ngrok 检查器)时,POST 正文出现了,所以一定是我的 django 服务器上配置不正确。
我在 settings.py 中设置了 ALLOWED_HOSTS = ['myNgrokAdress.ngrok.io', 'localhost']
。我还需要做些什么吗?
我在这里错过了什么?
问题出在 req.POST
方法上。
来自Django docs :
HttpRequest.POST:
A dictionary-like object containing all given HTTP POST parameters, providing that the request contains form data. See the QueryDict documentation below. If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead.
由于发送的数据类型是非格式数据,您需要使用req.body
。