为什么 post 请求在本地机器上工作但在 EC2 机器上失败?
Why post request is working on local machine but in EC2 machine fails?
当我尝试通过下面显示的视图文件在我的远程计算机上执行 post 请求时,我收到了一个 HTTP 400 错误请求,但是,当我使用我的本地计算机执行相同的操作时,它完美运行。
远程机器(在 EC2 机器上使用 docker)
- django == 2.2.1
- drf == 3.9.4
- python == 3.7
serializers.py
from rest_framework import serializers
from .models import irrigation, moisture
class IrrigationSerializer(serializers.ModelSerializer):
class Meta:
model = irrigation
fields = '__all__'
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
from .models import irrigation, moisture
from .serializers import IrrigationSerializer
from rest_framework import viewsets
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view, action
class irrigationValues(viewsets.ModelViewSet):
queryset = irrigation.objects.all()
serializer_class = IrrigationSerializer
def list(self, request):
serializer = self.serializer_class(self.queryset, many = True)
return Response(serializer.data)
def dashboard(request):
irrigations = irrigation.objects.all()
return render(request, 'dashboard.html',{'irrigations':irrigations})
@api_view(['POST'])
def addIrrigation(request):
addIrrigation = IrrigationSerializer(data=request.data)
print(request.data)
if addIrrigation.is_valid():
addIrrigation.save()
return Response({"data":"Value added"},status = status.HTTP_201_CREATED)
else:
error_details = []
for key in addIrrigation.errors.keys():
error_details.append({"field": key, "message": addIrrigation.errors[key][0]})
data = {
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": error_details
}
}
return Response(data, status=status.HTTP_400_BAD_REQUEST)
我从本地机器得到的结果是:
{
"data":"Value added"
}
"print(request.data)" 的输出是:
{'hardware_id': '001', 'temperature': 23, 'valve_status': 'Close'}
[02/Jul/2019 11:38:35] "POST /dashboard/addirrigation/ HTTP/1.1" 201 5680
我从远程机器得到的结果是:
{
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": [
{
"field": "hardware_id",
"message": "This field is required."
},
{
"field": "temperature",
"message": "This field is required."
},
{
"field": "valve_status",
"message": "This field is required."
}
]
}
}
"print(request.data)" 的输出是:
<QueryDict: {'_content_type': ['application/json'], '_content': ['{\r\n "hardware_id": "001",\r\n "temperature": 23,\r\n "valve_status": "Close"\r\n}']}>Bad Request: /dashboard/addirrigation/
注意:也许这对你来说很明显,但我需要提一下,当我使用 "HTML form" 通过 drf 执行 post 请求时,post 已成功提交,尽管如此,当我使用 "Raw data" 选项来 post 它时它不起作用。
Picture of the result
如问题所附图片所示,当我尝试通过 API 门户添加数据时,我收到了 HTTP 400 响应,但是当我使用邮递员并选择查询的内容类型时"application/json" 数据已成功发布(HTTP 201 响应)。
当我尝试通过下面显示的视图文件在我的远程计算机上执行 post 请求时,我收到了一个 HTTP 400 错误请求,但是,当我使用我的本地计算机执行相同的操作时,它完美运行。
远程机器(在 EC2 机器上使用 docker)
- django == 2.2.1
- drf == 3.9.4
- python == 3.7
serializers.py
from rest_framework import serializers
from .models import irrigation, moisture
class IrrigationSerializer(serializers.ModelSerializer):
class Meta:
model = irrigation
fields = '__all__'
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse
from .models import irrigation, moisture
from .serializers import IrrigationSerializer
from rest_framework import viewsets
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view, action
class irrigationValues(viewsets.ModelViewSet):
queryset = irrigation.objects.all()
serializer_class = IrrigationSerializer
def list(self, request):
serializer = self.serializer_class(self.queryset, many = True)
return Response(serializer.data)
def dashboard(request):
irrigations = irrigation.objects.all()
return render(request, 'dashboard.html',{'irrigations':irrigations})
@api_view(['POST'])
def addIrrigation(request):
addIrrigation = IrrigationSerializer(data=request.data)
print(request.data)
if addIrrigation.is_valid():
addIrrigation.save()
return Response({"data":"Value added"},status = status.HTTP_201_CREATED)
else:
error_details = []
for key in addIrrigation.errors.keys():
error_details.append({"field": key, "message": addIrrigation.errors[key][0]})
data = {
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": error_details
}
}
return Response(data, status=status.HTTP_400_BAD_REQUEST)
我从本地机器得到的结果是:
{
"data":"Value added"
}
"print(request.data)" 的输出是:
{'hardware_id': '001', 'temperature': 23, 'valve_status': 'Close'}
[02/Jul/2019 11:38:35] "POST /dashboard/addirrigation/ HTTP/1.1" 201 5680
我从远程机器得到的结果是:
{
"Error": {
"status": 400,
"message": "Your submitted data was not valid - please correct the below errors",
"error_details": [
{
"field": "hardware_id",
"message": "This field is required."
},
{
"field": "temperature",
"message": "This field is required."
},
{
"field": "valve_status",
"message": "This field is required."
}
]
}
}
"print(request.data)" 的输出是:
<QueryDict: {'_content_type': ['application/json'], '_content': ['{\r\n "hardware_id": "001",\r\n "temperature": 23,\r\n "valve_status": "Close"\r\n}']}>Bad Request: /dashboard/addirrigation/
注意:也许这对你来说很明显,但我需要提一下,当我使用 "HTML form" 通过 drf 执行 post 请求时,post 已成功提交,尽管如此,当我使用 "Raw data" 选项来 post 它时它不起作用。
Picture of the result
如问题所附图片所示,当我尝试通过 API 门户添加数据时,我收到了 HTTP 400 响应,但是当我使用邮递员并选择查询的内容类型时"application/json" 数据已成功发布(HTTP 201 响应)。