0

**Hi when I try to hit the post api from postman I am receiving the response from API but the request is not received from postman when I print the request it showing but not the data and i attached the screenshot of postman ** enter image description here

from django.shortcuts import render
from django.http import HttpResponse
from django.core.mail import send_mail
from django.views.decorators.csrf import csrf_exempt
# Create your views here.

@csrf_exempt
def email(request):
  print(request)
  subject = request.POST.get('subject', 'hello')
  body = request.POST.get('body', 'very bad')
  senderEmail = request.POST.get('senderEmail', 'my_email@gmail.com') 
  send_mail(subject, body, 'sender@gmail.com', [senderEmail], fail_silently=False)
  return HttpResponse("Email Send.")
Abishek
  • 47
  • 1
  • 1
  • 7

2 Answers2

0

I'm not sure what the context of your project is, but you can use serializers and write something like: return Response(serializer.initial_data, status=status.HTTP_200_OK). The serializer.initial_data would be the data you are initially sending, and it would show up in your Postman response box. Hope this helps. (You can read more about serializers for Django here).

Jessica
  • 1,083
  • 2
  • 12
  • 27
  • Can you share your `urls.py`? Also, you should add `@api_view['POST']` at the top of your function name so it can receive POST requests. – Jessica Dec 14 '19 at 04:04
  • This my urls.py ` from django.contrib import admin from django.urls import path, include from . import views urlpatterns = [ path('', views.index, name='index'), path('send/', include('mail.urls')), path('admin/', admin.site.urls), ] ` mail.urls ` from django.urls import path from . import views urlpatterns = [ path('', views.email, name="email"), ] ` – Abishek Dec 14 '19 at 04:07
0

Instead of raw data , choose a form data in a postman. enter image description here

In a python code you can change your code like below:

@api_view(['POST'])
@csrf_exempt
def email(request):
    formDataReceved = request.POST
    subjectReceived = formDataReceved['subject'].strip()
    bodyReceived = formDataReceved['body'].strip()
    senderEmailReceived = formDataReceved['senderEmail'].strip()
    send_mail(subjectReceived, bodyReceived, 'sender@gmail.com', [senderEmailReceived], fail_silently=False)
    return HttpResponse("Email Send.")
Deepa MG
  • 188
  • 1
  • 15