📜  cors github - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:00:02.256000             🧑  作者: Mango

CORS Github - Shell/Bash

CORS (Cross-Origin Resource Sharing) is a mechanism that allows resources on a website to be requested from another domain outside the domain from which the resource originated.

The CORS Github Shell/Bash script is designed to help developers with CORS-related issues that arise when working with APIs hosted on Github. This script helps in making HTTP requests to Github APIs by setting CORS headers.

Getting started

To use the CORS Github shell/bash script, follow these steps:

  1. Clone the repository To get started, clone this repository by running the following command in your terminal:
git clone https://github.com/BhavyaCodes/cors-github.git
  1. Install dependencies This script requires the curl command-line tool to be installed on your system. If you do not have curl installed, you can install it by running the following command:
sudo apt-get install curl
  1. Run the script To use the script, run the following command:
./cors-github.sh "https://api.github.com/users/BhavyaCodes"

This will make a GET request to the Github API for information about the user "BhavyaCodes".

Script explanation

The cors-github.sh script works by making an HTTP GET request to the given URL and setting the necessary CORS headers to allow the response to be retrieved by the browser.

Here is a breakdown of the script:

#!/bin/bash

url=$1
response=$(curl --write-out %{http_code} --silent --output /dev/null -H "Origin: https://example.com" $url)
# Make an HTTP GET request to the given URL with the 'Origin' header set to https://example.com

if [[ "$response" -eq 200 ]]
then
  echo "CORS headers added successfully!"
  curl -H "Origin: https://example.com" $url
  # If the request returns a 200 status code, print a success message and make another request with the 'Origin' header set to https://example.com
else
  echo "Failed to add CORS headers!"
  echo "Got response code: $response"
  # If the request does not return a 200 status code, print an error message and the status code returned by the request
fi

The url variable is initialized with the URL that will be passed in as an argument to the script.

The curl command is used to make an HTTP GET request to the given URL with the 'Origin' header set to https://example.com. The --write-out option is used to print the HTTP response status code to the console, and the --silent and --output /dev/null options are used to suppress the output of the actual response.

The status code returned by the request is stored in the response variable.

If the request returned a 200 status code, the script prints a success message and makes another request to the same URL with the 'Origin' header set to https://example.com.

If the request did not return a 200 status code, the script prints an error message along with the status code returned by the request.

Conclusion

The CORS Github shell/bash script is a simple and effective way to resolve CORS issues when making HTTP requests to Github APIs. By following the steps outlined in this guide, you can quickly and easily get started using this script in your own development projects.