Reputation: 59
user is sending an api request to crate order along with information in request body and after saving the data i am returning the order_id and access_token to the user.I have used few authentications also with using model.full_clean().Now i am stuck on the part to return success and failure status code and message along with the order_id and access_token.
@api_view(['POST'])
def orderdetails(request):
try:
ACCESS_KEY_ID = request.META.get('HTTP_ACCESS_KEY_ID')
ACCESS_KEY_SECRET = request.META.get('HTTP_ACCESS_KEY_SECRET')
applications = Applications.objects.all()
id=0
for e in applications:
if(e.ACCESS_KEY_ID==ACCESS_KEY_ID and e.ACCESS_KEY_SECRET==ACCESS_KEY_SECRET ):
id = e.id+id
print(id)
break
else:
return Response({"Message":"Enter Valid Credentials"})
except ValueError:
return Response({"ACCESS":"DENIED"})
if request.method == 'POST':
data=request.data
print(data)
orders = Orders(applications=Applications.objects.get(id=id),
purpose_code = data['purpose_code'],
amount=data['amount'],
currency=data['currency'],
note=data['note'],
payer_name=data['payer_name'],
payer_email=data['payer_email'],
payer_phone_country_code=data['payer_phone_country_code'],
payer_phone_number=data['payer_phone_number'],
payee_name=data['payee_name'],
payee_email=data['payee_email'],
payee_phone_country_code=data['payee_phone_country_code'],
payee_phone_number=data['payee_phone_number'],
payee_pan = data['payee_pan'],
payee_bank_account_holder_name=data['payee_bank_account_holder_name'],
payee_bank_account_number=data['payee_bank_account_number'],
payee_bank_account_ifsc=data['payee_bank_account_ifsc'],
payee_bank_account_type=data['payee_bank_account_type'],
payee_bank_account_beneficiary_identifier=data['payee_bank_account_beneficiary_identifier'],
payment_collection_webhook_url=data['payment_collection_webhook_url'],
payment_transfer_webhook_url=data['payment_transfer_webhook_url'],
payment_gateway_code = data['payment_gateway_code'],
isActive=data['isActive']
)
try:
orders.full_clean()
except ValidationError:
return Response({"Error message":"invalid request body"})
else:
orders.save()
serializer = OrderSerializer(orders)
order_id = serializer.data['id']
access_token = serializer.data['access_token']
return Response({"orderId":order_id,"accessToken":access_token})
required Success and failure response in the format bellow : Success response (201/202) along with order_id and access_token Failure response (422) along with message: "error message content"
Upvotes: 2
Views: 4195
Reputation: 381
You can return different HTTP status codes in the response.
# Importing status
from rest_framework import status
# To return a Successful response
# content is the data you return in the response, in your case its
content = {"orderId":order_id,"accessToken":access_token}
return Response(content, status=status.HTTP_200_OK)
# To return a Bad Request response
content = {"Error message":"invalid request body"}
return Response(content, status=status.HTTP_400_BAD_REQUEST)
You can find all the different HTTP Status codes for DRF here
Upvotes: 1
Reputation: 494
You could pass status code into Response object like below:
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['POST'])
def orderdetails(request):
return Response({'foo':'bar'}, status=status.HTTP_200_OK)
Upvotes: 1