Local CORS Proxy - Bypass CORS Restrictions

Create your own local CORS proxy server to bypass CORS restrictions when making API requests.

Flask CORS Proxy Server

from flask import Flask, request, Response
import requests
import json

app = Flask(__name__)

@app.route('/<path:url>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'])
def proxy(url):
    """
    Simple proxy server to bypass CORS restrictions.
    Usage: http://localhost:5000/https://api.example.com/endpoint
    """
    # Construct the target URL
    if not url.startswith(('http://', 'https://')):
        url = 'https://' + url
    
    # Forward the request method
    method = request.method
    
    # Forward the request headers
    headers = {key: value for key, value in request.headers.items() 
               if key.lower() not in ['host', 'content-length']}
    
    # Forward the request body
    data = request.get_data()
    
    try:
        # Make the request to the target URL
        response = requests.request(
            method=method,
            url=url,
            headers=headers,
            data=data,
            params=request.args,
            stream=True,
            verify=True  # Set to False to ignore SSL certificate errors
        )
        
        # Create a Flask response object
        flask_response = Response(
            response.iter_content(chunk_size=1024),
            status=response.status_code
        )
        
        # Forward the response headers
        for key, value in response.headers.items():
            if key.lower() not in ['content-encoding', 'content-length', 'transfer-encoding']:
                flask_response.headers[key] = value
        
        # Add CORS headers
        flask_response.headers['Access-Control-Allow-Origin'] = '*'
        flask_response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, PATCH, OPTIONS'
        flask_response.headers['Access-Control-Allow-Headers'] = '*'
        
        return flask_response
    
    except Exception as e:
        return json.dumps({"error": str(e)}), 500, {'Content-Type': 'application/json'}

@app.route('/', methods=['GET'])
def home():
    return """
    <h1>Local CORS Proxy</h1>
    <p>Usage: http://localhost:5000/https://api.example.com/endpoint</p>
    <p>This proxy will forward your request to the target URL and return the response with CORS headers.</p>
    """

if __name__ == '__main__':
    app.run(debug=True)

requirements.txt

flask==2.0.1
requests==2.26.0