Jin Nii Sama
Jin Nii Sama

Reputation: 747

Django calling API from view and nested POST

I am trying to do a POST method into the api from another project.

Firstly, i do a post to create appointment. If appointment success, i will post the current user profile into the Patient table from another project.

How do i do a POST in a POST and also how to post the current user information ? I tried to do it, as shown below on my code but its just bad.

The field i am posting into Patient table.
My user profile field ---> Patient table
userId > patientId
first_name > first_name
last_name > last_name
email > email

Here is my code:

views:

@csrf_exempt
def my_django_view(request):

    # request method to api from another project
    if request.method == 'POST':
        r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
    else:
        r = requests.get('http://127.0.0.1:8000/api/makeapp/', data=request.GET)

    if r.status_code == 201 and request.method == 'POST':
        data = r.json()
        print(data)

        # Have to post the user profile information into Patient table in another project.
        user_profile_attrs = {
            "patientId": self.request.userId,
            "first_name": self.request.first_name,
            "last_name": self.request.last_name,
            "username": self.request.username,
            "email": self.request.email,
        }
        # save into patient table of another project
        save_profile = requests.post('http://127.0.0.1:8000/api/patient/', data=request.POST)


        return HttpResponse(r.text)
    elif r.status_code == 200:  # GET response
        return HttpResponse(r.json())
    else:
        return HttpResponse(r.text)

Upvotes: 2

Views: 4283

Answers (2)

Aneesh R S
Aneesh R S

Reputation: 3827

issues

  1. requests.get passes arguments in params not in data. for detail click here
  2. use request.POST.get('userId') to get data from request, not request.userId

...

@csrf_exempt
def my_django_view(request):

    if request.method == 'POST':
        r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
    else:
        r = requests.get('http://127.0.0.1:8000/api/makeapp/', params=request.GET)

    if r.status_code == 201 and request.method == 'POST':
        data = r.json()
        print(data)

        # Have to post the user profile information into Patient table in another project.
        user_profile_attrs = {
            "patientId": request.POST.get('userId'),
            "first_name": request.POST.get('first_name'),
            "last_name": request.POST.get('last_name'),
            "username": request.POST.get('username'),
            "email": request.POST.get('email'),
        }
        # save into patient table of another project
        save_profile = requests.post('http://127.0.0.1:8000/api/patient/', data=request.POST)


        return HttpResponse(r.text)
    elif r.status_code == 200:  # GET response
        return HttpResponse(r.json())
    else:
        return HttpResponse(r.text)

Upvotes: 4

The_Cthulhu_Kid
The_Cthulhu_Kid

Reputation: 1859

One way would be:

@csrf_exempt
def my_django_view(request):

# request method to api from another project
if request.method == 'POST':
    r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
    if r.status_code == 201:
        data = r.json()
        print(data)

        # Have to post the user profile information into Patient table in another project.
        user_profile_attrs = {
            "patientId": request.POST.get('userId'),  # or request.user.id
            "first_name": request.POST.get('first_name'),  # or request.user.first_name
            "last_name": request.POST.get('last_name'),  # or request.user.last_name
            "username": request.POST.get('username'),  # or request.user.username
            "email": request.POST.get('email'),  # or request.user.email
        }
        # save into patient table of another project
        save_profile = requests.post('http://127.0.0.1:8000/api/patient/', data=request.POST)

        return HttpResponse(r.text)
else:
    r = requests.get('http://127.0.0.1:8000/api/makeapp/', data=request.GET)


if r.status_code == 200:  # GET response
    return HttpResponse(r.json())
else:
    return HttpResponse(r.text)

But you might be better using a class based view and splitting the POST and GET requests.

Also, remove self. as it has no meaning here.

[EDIT: Updated to access the request data correctly. If the user info isn't passed as args then the commented access is needed. Modified due to @AneeshRS' answer]

Upvotes: 1

Related Questions