2

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)
4
  • What error are you getting? Commented Feb 15, 2018 at 4:45
  • use import requests as req and req.post(/../patient, params=request.POST) Commented Feb 15, 2018 at 4:46
  • @Vaibhav that won't make any difference. Commented Feb 15, 2018 at 4:51
  • All the self.request.xxx are in red line. Commented Feb 15, 2018 at 4:53

2 Answers 2

4

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Damnit I missed that.
Thanks for the help and explanation.
1

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]

1 Comment

Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.