With this curl command I am requesting for only "Books" for field "ProductName"
curl -X POST http://localhost:8000/Productfilter/ -d '{"ProductName":"Books"}' -H "Content-Type:application/json"
Api code:
def Productfilter(self, request, format=None)
queryset = Model.objects.filter(ProductName=request['ProductName'])
ser = ModelSerializer(queryset, many=True)
for item in ser.data:
sendData.append({"ProductUrl": item['ProductUrl']}]
return sendData
I want to implement for multiple request values for same field. Like I want something like this--->
curl -X POST http://localhost:8000/Productfilter/ -d '{"ProductName":"Books","Pencil","Copy"}' -H "Content-Type:application/json"
Please suggest the curl command for this type of requests and what modification to be done in coding of def Productfilter
.
views.py
class Productfilter(APIView):
def post(self, request, format=None):
request.data['PubIp'] = getUserIP(request)
returnData = ApiFiltReq.Productfilter(self, request.data, format=None)
if returnData == "RECORD_NOT_FOUND": # bad request
return Response(ErrorCodes.ERROR_CODE_LIST[returnData], status=status.HTTP_400_BAD_REQUEST)
elif returnData == "DJANGO_ENTRY_FAILED":
return Response(ErrorCodes.ERROR_CODE_LIST[returnData], status=status.HTTP_400_BAD_REQUEST)
else:
return Response(returnData, status=status.HTTP_202_ACCEPTED)
First thing, if you're only asking for data you should use a GET request.
Second thing, I assume that this ProductName=request['ProductName']
is a typo and you're really using request.POST[xxx]
(which should be request.GET[xxx]
actually cf first point above)
wrt/ how you can get multiple values for the same key on the Django side, you just have to use request.GET.getlist(xxx)
and change your query to Model.objects.filter(ProductName__in=yyy)
:
def Productfilter(self, request, format=None)
names = request.GET.getlist('ProductName')
queryset = Model.objects.filter(ProductName__in=names)
# etc
for the cURL part you'll have to check the FineManual but IIRC this should work:
curl -G http://localhost:8000/Productfilter/ --d ProductName=Books -d ProductName=Pencil -d ProductName=Copy
or just pass a plain querystring in your url:
curl -G http://localhost:8000/Productfilter/?ProductName=Books&ProductName=Pencil&ProductName=Copy
Try this:
def Productfilter(self, request, format=None)
queryset = Model.objects.filter(ProductName=request.data['ProductName'], Pencil=request.data['Pencil'])
ser = ModelSerializer(queryset, many=True)
for item in ser.data:
sendData.append({"ProductUrl": item['ProductUrl']}]
return sendData
and the curl command will look like this:
curl -X POST \
http://localhost:8000/Productfilter/ \
-d ProductName=Books
-d Pencil=Copy
-H "Content-Type:application/json"
Hope it helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.