Created
November 4, 2022 01:49
-
-
Save markusand/5aaebbcb4f9c33767cdb65ecef674347 to your computer and use it in GitHub Desktop.
HTTP Proxy to redirect GET requests to POST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
from http.server import BaseHTTPRequestHandler, HTTPServer | |
from urllib import parse | |
from requests import post | |
port = os.environ['PROXY_PORT'] | |
url = os.environ['FORWARD_URL'] | |
token = os.environ['TOKEN'] | |
class Proxy(BaseHTTPRequestHandler): | |
def do_GET(self): | |
params = dict(parse.parse_qsl(self.path[2:])) | |
headers = {} # {'Authorization': 'Bearer {}'.format(token)} | |
response = post(url, headers=headers, data=json.dumps(params)) | |
self.send_response(response.status_code) | |
self.send_response(200) | |
self.send_header('Content-Type', 'application/json') | |
self.end_headers() | |
self.wfile.write(json.dumps(response.json()).encode(encoding='utf_8')) | |
httpd = HTTPServer(('', port), Proxy) | |
print('Proxy running in port', port) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment