我正在开发要接收POST请求的网站中实现Webhook接收器。我在接收POST请求时遇到问题。为了测试它是否正常运行,我测试了使用curl发送POST数据。在本地开发服务器上运行正常,但是在服务器上进行测试时,我收到301 Moved Permanently错误。
如果我正在下面的本地开发服务器上进行测试
curl --data "event="CASHGRAM_REDEEMED"&cashgramid="cashgramid"&referenceId="referenceId"&eventTime="now"&utr="utr_1"&signature="GHxFF2JHuFVRg0zSJ4L82ju0QtQx3DAEAy9hF4eLzEs="" http://127.0.0.1:8000/cashfree_cashgram_webhook/
工作正常。但是当我这样做来测试代码何时在服务器上
curl --data "event="CASHGRAM_REDEEMED"&cashgramid="cashgramid"&referenceId="referenceId"&eventTime="now"&utr="utr_1"&signature="BHvFF2JDuFVTh0zSJ4L82ju0QtQx3DAEAy9hF4eLzEs="" https://mywebsite.com/cashfree_cashgram_webhook/
那我就跌倒了。
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.mywebsite.com/cashfree_cashgram_webhook/">here</a>.</p>
</body></html>
出于隐私考虑,我用mywesite.com替换了真实的网站地址
以下是后端中用于接收POST请求的view方法。通过这种方法,我将验证签名并保存接收到的信息。
@csrf_exempt
def cashfree_cashgram_webhook(request):
if request.method == "POST":
data = ""
for key in sorted(request.POST.keys()):
print(request.POST[key])
if key != 'signature':
data = data+key+request.POST[key]
secretKey = settings.CLIENT_SECRET
secretKey = bytes(secretKey, 'utf-8')
data = bytes(data, 'utf-8')
dig = hmac.new(key=secretKey,
msg=data,
digestmod=hashlib.sha256)
generated_signature = base64.b64encode(dig.digest())
generated_signature = generated_signature.decode('utf-8')
# verify signature
if generated_signature == request.POST['signature']:
if request.POST['event'] == "CASHGRAM_REDEEMED":
event = request.POST['event']
cashgramid = request.POST['cashgramid']
referenceId = request.POST['referenceId']
eventTime = request.POST['eventTime']
utr = request.POST['utr']
if not cashgram.objects.filter(event=event,cashgramid=cashgramid,referenceId=referenceId,eventTime=eventTime,utr =utr).exists():
cashgram_obj = cashgram(event=event,cashgramid=cashgramid,referenceId=referenceId,eventTime=eventTime,utr =utr)
cashgram_obj.save()
if request.POST['event'] == "CASHGRAM_TRANSFER_REVERSAL":
event = request.POST['event']
cashgramid = request.POST['cashgramid']
referenceId = request.POST['referenceId']
eventTime = request.POST['eventTime']
if not cashgram.objects.filter(event=event,cashgramid=cashgramid,referenceId=referenceId,eventTime=eventTime).exists():
cashgram_obj = cashgram(event=event,cashgramid=cashgramid,referenceId=referenceId,eventTime=eventTime)
cashgram_obj.save()
if request.POST['event'] == "CASHGRAM_EXPIRED":
event = request.POST['event']
cashgramid = request.POST['cashgramid']
reason = request.POST['reason']
if not cashgram.objects.filter(event=event,cashgramid=cashgramid,reason=reason).exists():
ccashgram_obj = cashgram(event=event,cashgramid=cashgramid,reason=reason)
cashgram_obj.save()
return HttpResponse(status=200)
有人可以帮我弄清楚为什么它不能在服务器上工作吗?