From 5b995459b8518a251cd7a368ca6bdaa1dfeb12f1 Mon Sep 17 00:00:00 2001 From: Silvia Pasciullo <117067632+silviapasc@users.noreply.github.com> Date: Fri, 8 Aug 2025 19:23:50 +0200 Subject: [PATCH 1/3] [Term Entry] Python Requests Module: .patch() --- .../requests-module/terms/patch/patch.md | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 content/python/concepts/requests-module/terms/patch/patch.md diff --git a/content/python/concepts/requests-module/terms/patch/patch.md b/content/python/concepts/requests-module/terms/patch/patch.md new file mode 100644 index 00000000000..e6b21031d5b --- /dev/null +++ b/content/python/concepts/requests-module/terms/patch/patch.md @@ -0,0 +1,133 @@ +--- +Title: '.patch()' +Description: 'Sends a PATCH request to a specified URL.' +Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Methods' + - 'Modules' + - 'Python' +CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first + - 'learn-example-course' + - 'paths/example-path' +CatalogContent: +- 'learn-python-3' +- 'paths/computer-science' +--- + +The **.patch()** method in the `requests` module is used to send a `PATCH` request to a specified URL, allowing for partial modifications of a resource. When only a subset of data needs to be updated, this method is particularly efficient, as `PATCH` can modify specific fields without requiring the replacement of the entire resource, unlike `PUT` from the [`.put()`](https://www.codecademy.com/resources/docs/python/requests-module/put) method, which overwrites the entire object. + +## Syntax + +```pseudo +requests.patch(url, data=None, json=None, **kwargs) +``` + +**Parameters:** + +- `url`: URL to which the request is sent. +- `data`: (optional) Dictionary, list of tuples, bytes, or file-like object that contains the data to send. +- `json`: (optional) A JSON serializable Python object that will be sent as a JSON formatted string. +- `kwargs`: Additional keyword arguments that can be passed to the method (e.g. headers, authentication, etc.). + +**Return value:** + +The method returns a `Response` object, which contains various information about the HTTP response returned by the server after processing the `PATCH` request. + + +## Example 1 + +In this example, the `.path()` method is used to send a `PATCH` request to a REST API of a user management system in order to update a user's profile. The user's current data is modelled as a JSON object: + +```json +{ + "id": 53, + "name": "Lorem Ipsum", + "email": "lorem.ipsum@example.com" +} +``` + +We just want to update the user's email address without changing the other fields: + +```python +import requests + +# URL of the API endpoint for the user +url = 'https://api.example.com/users/53' + +# Updated user field +updated_data = { + "email": "loremipsum@example.com" +} + +# Sending the PATCH request +response = requests.patch(url, json=updated_data) + +# Checking the response +if response.status_code == 200: + updated_user = response.json() + print("User updated successfully:", updated_user) +else: + print("Failed to update user:", response.status_code, response.text) +``` + +A conditional `if-else` statement is used in this case to output a message indicating a successful data update, when the server status code is `200`, or an unsuccessful update otherwise. + +For a multiple fields update, it is sufficient to store all the attributes in the JSON object, as seen in the `updated_multi` variable, and pass it to the `patch()` method: + +```python +updated_multi = { + "email": "loremipsum@example.com", + "telephone": "+00123456789" +} + +response = requests.patch(url, json=updated_multi) +``` + +## Example 2 + +In the following example, custom headers (such as an Authorization token) are included in the `PATCH` request: + +```python +import requests + +url = "https://api.example.com/resource/17" + +data = { + "description": "This is an updated resource description." +} + +headers = { + "Authorization": "Bearer YOUR_ACCESS_TOKEN", + "Content-Type": "application/json" +} + +response = requests.patch(url, json=data, headers=headers) + +if response.status_code == 200: + print("Resource updated successfully:", response.json()) +else: + print("Failed to update resource, status code:", response.status_code) +``` + +## Codebyte Example + +In this example, the `patch()` method sends a basic `PATCH` request to a server to update the resource `newspaper/0123`: + +```codebyte/python +import requests + +# URL of the resource to be updated +url = 'https://api.example.com/newspaper/0123' + +# Data to be updated +data = {'name': 'Daily Newspaper'} + +# Sending a PATCH request +response = requests.patch(url, json=data) + +# Print the status code and response data +print(response.status_code) # Expecting 200 (OK) or 204 (No Content) if successful +print(response.json()) # Prints the updated resource details +``` From ee75eb475e20af17cf88fac0038b6300436908ae Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 14 Aug 2025 14:21:39 +0530 Subject: [PATCH 2/3] Update patch.md --- .../requests-module/terms/patch/patch.md | 71 ++++++------------- 1 file changed, 23 insertions(+), 48 deletions(-) diff --git a/content/python/concepts/requests-module/terms/patch/patch.md b/content/python/concepts/requests-module/terms/patch/patch.md index e6b21031d5b..482a5659229 100644 --- a/content/python/concepts/requests-module/terms/patch/patch.md +++ b/content/python/concepts/requests-module/terms/patch/patch.md @@ -1,22 +1,19 @@ --- Title: '.patch()' -Description: 'Sends a PATCH request to a specified URL.' -Subjects: # Please only use Subjects in the subjects.md file (https://github.com/Codecademy/docs/blob/main/documentation/subjects.md). If that list feels insufficient, feel free to create a new Subject and add it to subjects.md in your PR! +Description: 'Sends a PATCH request to a specified URL to partially update a resource.' +Subjects: - 'Code Foundations' - 'Computer Science' Tags: - 'Methods' - 'Modules' - 'Python' -CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first - - 'learn-example-course' - - 'paths/example-path' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -The **.patch()** method in the `requests` module is used to send a `PATCH` request to a specified URL, allowing for partial modifications of a resource. When only a subset of data needs to be updated, this method is particularly efficient, as `PATCH` can modify specific fields without requiring the replacement of the entire resource, unlike `PUT` from the [`.put()`](https://www.codecademy.com/resources/docs/python/requests-module/put) method, which overwrites the entire object. +The **`.patch()`** method in the `requests` module is used to send a `PATCH` request to a specified URL, allowing for partial modifications of a resource. When only a subset of data needs to be updated, this method is particularly efficient, as `PATCH` can modify specific fields without requiring the replacement of the entire resource, unlike `PUT` from the [`.put()`](https://www.codecademy.com/resources/docs/python/requests-module/put) method, which overwrites the entire object. ## Syntax @@ -29,28 +26,27 @@ requests.patch(url, data=None, json=None, **kwargs) - `url`: URL to which the request is sent. - `data`: (optional) Dictionary, list of tuples, bytes, or file-like object that contains the data to send. - `json`: (optional) A JSON serializable Python object that will be sent as a JSON formatted string. -- `kwargs`: Additional keyword arguments that can be passed to the method (e.g. headers, authentication, etc.). +- `kwargs`: Additional keyword arguments that can be passed to the method (e.g. `headers`, `auth`, etc.). **Return value:** The method returns a `Response` object, which contains various information about the HTTP response returned by the server after processing the `PATCH` request. - ## Example 1 -In this example, the `.path()` method is used to send a `PATCH` request to a REST API of a user management system in order to update a user's profile. The user's current data is modelled as a JSON object: +In this example, the `.patch()` method is used to send a `PATCH` request to a REST API of a user management system in order to update a user's profile. The user's current data is modeled as a JSON object: ```json { - "id": 53, - "name": "Lorem Ipsum", - "email": "lorem.ipsum@example.com" + "id": 53, + "name": "Lorem Ipsum", + "email": "lorem.ipsum@example.com" } ``` We just want to update the user's email address without changing the other fields: -```python +```py import requests # URL of the API endpoint for the user @@ -58,7 +54,7 @@ url = 'https://api.example.com/users/53' # Updated user field updated_data = { - "email": "loremipsum@example.com" + "email": "loremipsum@example.com" } # Sending the PATCH request @@ -66,20 +62,20 @@ response = requests.patch(url, json=updated_data) # Checking the response if response.status_code == 200: - updated_user = response.json() - print("User updated successfully:", updated_user) + updated_user = response.json() + print("User updated successfully:", updated_user) else: - print("Failed to update user:", response.status_code, response.text) + print("Failed to update user:", response.status_code, response.text) ``` A conditional `if-else` statement is used in this case to output a message indicating a successful data update, when the server status code is `200`, or an unsuccessful update otherwise. -For a multiple fields update, it is sufficient to store all the attributes in the JSON object, as seen in the `updated_multi` variable, and pass it to the `patch()` method: +For a updating multiple fields, it is sufficient to store all the attributes in the JSON object, as seen in the `updated_multi` variable, and pass it to the `patch()` method: -```python +```py updated_multi = { - "email": "loremipsum@example.com", - "telephone": "+00123456789" + "email": "loremipsum@example.com", + "telephone": "+00123456789" } response = requests.patch(url, json=updated_multi) @@ -89,45 +85,24 @@ response = requests.patch(url, json=updated_multi) In the following example, custom headers (such as an Authorization token) are included in the `PATCH` request: -```python +```py import requests url = "https://api.example.com/resource/17" data = { - "description": "This is an updated resource description." + "description": "This is an updated resource description." } headers = { - "Authorization": "Bearer YOUR_ACCESS_TOKEN", - "Content-Type": "application/json" + "Authorization": "Bearer YOUR_ACCESS_TOKEN", + "Content-Type": "application/json" } response = requests.patch(url, json=data, headers=headers) if response.status_code == 200: - print("Resource updated successfully:", response.json()) + print("Resource updated successfully:", response.json()) else: - print("Failed to update resource, status code:", response.status_code) -``` - -## Codebyte Example - -In this example, the `patch()` method sends a basic `PATCH` request to a server to update the resource `newspaper/0123`: - -```codebyte/python -import requests - -# URL of the resource to be updated -url = 'https://api.example.com/newspaper/0123' - -# Data to be updated -data = {'name': 'Daily Newspaper'} - -# Sending a PATCH request -response = requests.patch(url, json=data) - -# Print the status code and response data -print(response.status_code) # Expecting 200 (OK) or 204 (No Content) if successful -print(response.json()) # Prints the updated resource details + print("Failed to update resource, status code:", response.status_code) ``` From d3ac7d76d05a6aa620e55cd8fe7035a7413c99c5 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 14 Aug 2025 14:24:45 +0530 Subject: [PATCH 3/3] Update patch.md --- .../requests-module/terms/patch/patch.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/content/python/concepts/requests-module/terms/patch/patch.md b/content/python/concepts/requests-module/terms/patch/patch.md index 482a5659229..1b492230b4b 100644 --- a/content/python/concepts/requests-module/terms/patch/patch.md +++ b/content/python/concepts/requests-module/terms/patch/patch.md @@ -106,3 +106,23 @@ if response.status_code == 200: else: print("Failed to update resource, status code:", response.status_code) ``` + +## Codebyte Example + +In this example, the `.patch()` method sends a `PATCH` request to a public test API to update a resource’s title: + +```codebyte/python +import requests + +# Public test API endpoint (JSONPlaceholder) +url = 'https://jsonplaceholder.typicode.com/posts/1' + +# Data to update +data = {'title': 'Updated Title'} + +# Sending the PATCH request +response = requests.patch(url, json=data) + +print(response.status_code) # Expect 200 if successful +print(response.json()) # Prints simulated updated resource +```