Repository Operations with Postman github with Postman - project 2 - Notes By ShariqSP

Repository Operations with Postman

This guide explains the detailed steps to perform repository operations like creating, updating, and deleting a repository using Postman with a REST API. Each request type will be explained with steps for setup, request format, and expected response.

1. Create a Repository

To create a new repository, we use a POST request.

  • Method: POST
  • Endpoint: https://api.github.com/user/repos
  • Headers: Authorization: Bearer [YOUR_TOKEN], Content-Type: application/json

Steps:

  1. Open Postman and select POST as the request type.
  2. Enter the endpoint URL https://api.github.com/user/repos.
  3. In the Headers tab, add:
    • Authorization: Bearer [YOUR_TOKEN]
    • Content-Type: application/json
  4. In the Body tab, select raw and JSON, and add:
    {
                    "name": "new-repo-name",
                    "description": "Description of the repository",
                    "private": false
                }
  5. Click Send.

Explanation: This request creates a new repository. The Authorization header authenticates the request, and the name parameter specifies the repository’s name.

For detailed API specifications, see the Swagger Documentation for Creating a Repository.

2. Delete a Repository

To delete a repository, we use a DELETE request.

  • Method: DELETE
  • Endpoint: https://api.github.com/repos/[username]/[repo-name]
  • Headers: Authorization: Bearer [YOUR_TOKEN]

Steps:

  1. In Postman, select DELETE as the request type.
  2. Enter the endpoint URL https://api.github.com/repos/[username]/[repo-name] with your username and repository name.
  3. Add the Authorization header with the token.
  4. Click Send.

Explanation: This request deletes the specified repository. The DELETE method in combination with the specific repository URL tells the API to permanently remove the repository.

For detailed API specifications, see the Swagger Documentation for Deleting a Repository.

3. List Repositories

To list all repositories, we use a GET request.

  • Method: GET
  • Endpoint: https://api.github.com/user/repos
  • Headers: Authorization: Bearer [YOUR_TOKEN]

Steps:

  1. Select GET as the request type.
  2. Enter the endpoint URL https://api.github.com/user/repos.
  3. Add the Authorization header with the token.
  4. Click Send.

Explanation: This request retrieves all repositories for the authenticated user. The GET method pulls data, and the token confirms the user’s identity.

For detailed API specifications, see the Swagger Documentation for Listing Repositories.

4. Update Repository Information

To update repository information, we use a PATCH request.

  • Method: PATCH
  • Endpoint: https://api.github.com/repos/[username]/[repo-name]
  • Headers: Authorization: Bearer [YOUR_TOKEN], Content-Type: application/json

Steps:

  1. Select PATCH as the request type.
  2. Enter the endpoint URL with your username and repository name.
  3. In Headers, add:
    • Authorization: Bearer [YOUR_TOKEN]
    • Content-Type: application/json
  4. In the Body tab, select raw and JSON, and add:
    {
                    "name": "updated-repo-name",
                    "description": "Updated description"
                }
  5. Click Send.

Explanation: This updates the repository’s details. The PATCH method allows partial updates to the repository, such as changing the name or description.

For detailed API specifications, see the Swagger Documentation for Updating a Repository.

Storing and Using Response Data in Postman

This guide explains how to store data from a response in a variable in Postman and then use that variable in a subsequent request. This technique is useful when you need to pass data from one request to another, such as an ID or token returned in the response.

Scenario: Creating and Retrieving a Repository

In this scenario, we’ll:

  1. Create a repository and store the repo_name from the response in a variable.
  2. Use that stored variable in a subsequent request to retrieve the details of the repository.

1. Step 1: Create Repository and Store Response Data

First, we’ll create a repository and store the repository name in a variable for later use.

  • Request Type: POST
  • Endpoint: https://api.github.com/user/repos
  • Headers: Authorization: Bearer [YOUR_TOKEN], Content-Type: application/json

Steps:

  1. Open Postman and select POST as the request type.
  2. Enter the endpoint URL https://api.github.com/user/repos.
  3. In the Headers tab, add:
    • Authorization: Bearer [YOUR_TOKEN]
    • Content-Type: application/json
  4. In the Body tab, select raw and JSON, and add:
    {
                    "name": "sample-repo",
                    "description": "This is a test repository"
                }
  5. In the Tests tab, add the following code to capture the repository name:
    
                // Store the repository name from the response
                let response = pm.response.json();
                pm.environment.set("repo_name", response.name);
                
  6. Click Send.

Explanation: This request creates a new repository named sample-repo. In the Tests tab, we capture the repository’s name from the response and store it in an environment variable named repo_name. This variable is now available for use in subsequent requests.

2. Step 2: Retrieve Repository Details Using Stored Variable

Next, we’ll use the repo_name variable to retrieve the details of the newly created repository.

  • Request Type: GET
  • Endpoint: https://api.github.com/repos/[username]/{{repo_name}}
  • Headers: Authorization: Bearer [YOUR_TOKEN]

Steps:

  1. In Postman, select GET as the request type.
  2. Enter the endpoint URL as https://api.github.com/repos/[username]/{{repo_name}}. Replace [username] with your GitHub username, and use {{repo_name}} as the placeholder for the variable.
  3. Add the Authorization header with the token.
  4. Click Send.

Explanation: The GET request retrieves the details of the repository by using {{repo_name}}, which references the repository name stored in the previous step. This demonstrates how Postman can pass data from one request to another using environment variables.

3. Tips for Using Variables in Postman

When working with variables in Postman:

  • Use pm.environment.set("variable_name", value) in the Tests tab to store data.
  • Access variables in requests using {{variable_name}} format.
  • Clear variables after testing with pm.environment.unset("variable_name") if needed.

For more details on working with variables, refer to the Postman Documentation on Variables.