🔐

Authorizing Requests in Postman

Jul 15, 2024

Authorizing Requests in Postman

Introduction

  • This video focuses on how to authorize a request in Postman, specifically using the GitHub API.
  • Pre-requisite: A GitHub account.

Setting Up

  • GitHub API Documentation: Follow the link in the video description to access the documentation.
  • Objective: Create a new repository using the GitHub API.

GitHub API Endpoint

  • Endpoint: /user/repos
  • Request Type: POST
  • Base URL: api.github.com

Making an Unauthorized Request

  • Open Postman and create a new POST request to api.github.com/user/repos
  • Initial submission without credentials returns 401 Unauthorized.
  • 401 Unauthorized: Indicates that credentials are needed.

Authentication with Tokens

  • Why Tokens: Provides limited access to account without using username and password.
  • Creating a Token: Steps
    • Go to GitHub account → Settings → Developer settings → Personal access tokens.
    • Click on 'Generate new token'.
    • Add a note for reference (e.g., Postman) and set an expiration date.
    • Select appropriate scopes (permissions), e.g., repo for repository-related actions.
    • Generate and copy the token.
    • Security Note: Never share the token.

Using Token in Postman

  • Authorization Helpers: Go to the Authorization tab in Postman.
  • Select 'Bearer Token' from the dropdown and paste the token.
  • Result: On sending the request, 401 Unauthorized changes to 400 Bad Request.
  • Reason for 400 Bad Request: Indicates that body should be a JSON object.

Constructing a Valid JSON Body

  • API Documentation: Check required parameters for the request body (e.g., name of the repository).
  • Postman Setup:
    • Go to the Body tab, select 'raw' and then select 'JSON'.
    • Create a JSON body with necessary parameters.
    • Example: {"name": "created-from-postman", "description": "Repository created using Postman"}
  • Successful Request: Returns 201 Created indicating the repository was successfully created.
  • Validation: Check on GitHub to confirm the repository creation.

Common Errors and Troubleshooting

  • Status Codes:
    • 400 Bad Request: Ensure body is a valid JSON object.
    • 404 Not Found: Double-check the endpoint URL.
    • 401 Unauthorized: Ensure token is correctly configured and valid.
  • JSON Errors: Solve parsing issues and ensure correct formatting.
  • Additional Help: Check video description for more troubleshooting ideas, or visit the Postman community for support.