From 66f66a1772fbdb2b10879e982c4423a2f24b8738 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 6 Mar 2024 05:10:47 -0500 Subject: [PATCH] Correct internal API logic to properly handle PUT/PATCH/DELETE requests --- .../test_app/templates/test_app/api_send.html | 5 +++- django_rest/test_app/views.py | 23 +++++++++++++------ .../test_app/templates/test_app/api_send.html | 5 +++- django_v4/test_app/views.py | 23 +++++++++++++------ 4 files changed, 40 insertions(+), 16 deletions(-) diff --git a/django_rest/test_app/templates/test_app/api_send.html b/django_rest/test_app/templates/test_app/api_send.html index 42ac53a..f0e098e 100644 --- a/django_rest/test_app/templates/test_app/api_send.html +++ b/django_rest/test_app/templates/test_app/api_send.html @@ -215,7 +215,10 @@ value="Submit as PUT" title="Generally used to send data to the server, and update an existing resource by full replacement." > - <input type="submit" name="submit_put" value="Submit as PATCH" + <input + type="submit" + name="submit_patch" + value="Submit as PATCH" title="Generally used to send data to the server, and update an existing resource by partial replacement." > <input diff --git a/django_rest/test_app/views.py b/django_rest/test_app/views.py index bd5b04c..dc6cf51 100644 --- a/django_rest/test_app/views.py +++ b/django_rest/test_app/views.py @@ -72,7 +72,7 @@ def view_with_group_check(request): # region API Views @csrf_exempt -@require_http_methods(["GET", "POST"]) +@require_http_methods(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) def api_parse(request): """Takes in JSON ping, and saves incoming value to web cookies. @@ -206,12 +206,6 @@ def api_send(request): response_error = {} sent_data = {} - # Get initial data. - form_initial = [{ - 'is_str': True, - 'value': 'This is a test entry.' - }] - # Initialize formset. form = ApiSendForm() @@ -380,6 +374,21 @@ def api_send(request): '404: Not Found - This error is often the result of the requested url not existing on the ' 'server. Are you sure you entered the destination URL correctly?' ) + elif response_success['status'] == 405: + # 405: Method Not Allowed + response_success['help_text'] = ( + '405: Method Not Allowed - This error is often the result of the destination understanding ' + 'the sent response type (GET/POST/PUT/PATCH/DELETE), but not supporting said type. ' + 'If this is a server you have access to, then double check that the endpoint is configured ' + 'correctly.' + ) + elif response_success['status'] == 415: + # 415: Unsupported Media Type + response_success['help_text'] = ( + '415: Unsupported Media Type - This error is often the result of the destination ' + 'being unable to parse the provided content. Are you sure the payload was entered ' + 'correctly?' + ) elif response_success['status'] == 500: # 500: Server Error response_success['help_text'] = ( diff --git a/django_v4/test_app/templates/test_app/api_send.html b/django_v4/test_app/templates/test_app/api_send.html index bf13d5f..48d1b13 100644 --- a/django_v4/test_app/templates/test_app/api_send.html +++ b/django_v4/test_app/templates/test_app/api_send.html @@ -215,7 +215,10 @@ value="Submit as PUT" title="Generally used to send data to the server, and update an existing resource by full replacement." > - <input type="submit" name="submit_put" value="Submit as PATCH" + <input + type="submit" + name="submit_patch" + value="Submit as PATCH" title="Generally used to send data to the server, and update an existing resource by partial replacement." > <input diff --git a/django_v4/test_app/views.py b/django_v4/test_app/views.py index 3f02107..150b54a 100644 --- a/django_v4/test_app/views.py +++ b/django_v4/test_app/views.py @@ -65,7 +65,7 @@ def view_with_group_check(request): # region API Views @csrf_exempt -@require_http_methods(["GET", "POST"]) +@require_http_methods(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']) def api_parse(request): """Takes in JSON ping, and saves incoming value to web cookies. @@ -199,12 +199,6 @@ def api_send(request): response_error = {} sent_data = {} - # Get initial data. - form_initial = [{ - 'is_str': True, - 'value': 'This is a test entry.' - }] - # Initialize formset. form = ApiSendForm() @@ -373,6 +367,21 @@ def api_send(request): '404: Not Found - This error is often the result of the requested url not existing on the ' 'server. Are you sure you entered the destination URL correctly?' ) + elif response_success['status'] == 405: + # 405: Method Not Allowed + response_success['help_text'] = ( + '405: Method Not Allowed - This error is often the result of the destination understanding ' + 'the sent response type (GET/POST/PUT/PATCH/DELETE), but not supporting said type. ' + 'If this is a server you have access to, then double check that the endpoint is configured ' + 'correctly.' + ) + elif response_success['status'] == 415: + # 415: Unsupported Media Type + response_success['help_text'] = ( + '415: Unsupported Media Type - This error is often the result of the destination ' + 'being unable to parse the provided content. Are you sure the payload was entered ' + 'correctly?' + ) elif response_success['status'] == 500: # 500: Server Error response_success['help_text'] = ( -- GitLab