Issue
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)
Solution
issues
- requests.get passes arguments in
params
not indata
. for detail click here - use
request.POST.get('userId')
to get data from request, notrequest.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)
Answered By - Aneesh R S
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.