Skip to content

Instantly share code, notes, and snippets.

@ssi-anik
Last active March 1, 2020 18:18
Show Gist options
  • Save ssi-anik/5caf23edf6b9d5f4da170dc9a36182bb to your computer and use it in GitHub Desktop.
Save ssi-anik/5caf23edf6b9d5f4da170dc9a36182bb to your computer and use it in GitHub Desktop.
function parser(url) {
return url.replace(/^\/(.*)$/g, '$1');
}
HOST = 'http://127.0.0.1:8080/';
function get(url = '/get') {
fetch(HOST + parser(url), {
method: 'GET',
}).then((data) => data.json()).then((data) => console.log(data));
}
function cget(url = '/get') {
fetch(HOST + parser(url), {
method: 'GET',
credentials: "include",
}).then((data) => data.json()).then((data) => console.log(data));
}
function get_header(url = '/get') {
fetch(HOST + parser(url), {
method: 'GET',
headers: {
'X-ALLOWED-1': 'allowed-1',
"Content-Type": "application/text",
'authorization': 'bearer auth-token'
},
credentials: "include",
}).then((data) => data.json()).then((data) => console.log(data));
}
function head(url = '/head') {
fetch(HOST + parser(url), {
method: 'HEAD',
}).then((data) => console.log('head request sent'));
}
function post(url = '/post') {
fetch(HOST + parser(url), {
method: "POST",
body: JSON.stringify({'post-field-1': 1, 'post-field-2': 2}),
}).then((data) => data.json()).then((data) => console.log(data));
}
function cpost(url = '/post') {
fetch(HOST + parser(url), {
method: "POST",
body: JSON.stringify({'post-field-1': 1, 'post-field-2': 2}),
credentials: "include"
}).then((data) => data.json()).then((data) => console.log(data));
}
function post_header(url = '/post') {
fetch(HOST + parser(url), {
method: "POST",
body: JSON.stringify({'post-field-1': 1, 'post-field-2': 2}),
headers: {
'X-ALLOWED-1': 'allowed-1',
"Content-Type": "application/text",
'authorization': 'bearer auth-token'
},
credentials: "include"
}).then((data) => data.json()).then((data) => console.log(data));
}
function put(url = '/put') {
fetch(HOST + parser(url), {
method: "PUT",
body: JSON.stringify({'PUT-field-1': 1, 'PUT-field-2': 2}),
}).then((data) => data.json()).then((data) => console.log(data));
}
function cput(url = '/put') {
fetch(HOST + parser(url), {
method: "PUT",
body: JSON.stringify({'PUT-field-1': 1, 'PUT-field-2': 2}),
credentials: "include"
}).then((data) => data.json()).then((data) => console.log(data));
}
function put_header(url = '/put') {
fetch(HOST + parser(url), {
method: "PUT",
body: JSON.stringify({'PUT-field-1': 1, 'PUT-field-2': 2}),
headers: {
'X-ALLOWED-1': 'allowed-1',
"Content-Type": "application/text",
},
credentials: "include"
}).then((data) => data.json()).then((data) => console.log(data));
}
function patch(url = '/patch') {
fetch(HOST + parser(url), {
method: "PATCH",
body: JSON.stringify({'patch-field-1': 1, 'patch-field-2': 2}),
credentials: "include"
}).then((data) => data.json()).then((data) => console.log(data));
}
function cpatch(url = '/patch') {
fetch(HOST + parser(url), {
method: "PATCH",
body: JSON.stringify({'patch-field-1': 1, 'patch-field-2': 2}),
}).then((data) => data.json()).then((data) => console.log(data));
}
function patch_header(url = '/patch') {
fetch(HOST + parser(url), {
method: "PATCH",
body: JSON.stringify({'patch-field-1': 1, 'patch-field-2': 2}),
headers: {
'X-ALLOWED-1': 'allowed-1',
"Content-Type": "application/text",
},
credentials: "include"
}).then((data) => data.json()).then((data) => console.log(data));
}
function del(url = '/delete') {
fetch(HOST + parser(url), {
method: "DELETE",
body: JSON.stringify({'delete-field-1': 1, 'delete-field-2': 2}),
}).then((data) => data.json()).then((data) => console.log(data));
}
function cdel(url = '/delete') {
fetch(HOST + parser(url), {
method: "DELETE",
body: JSON.stringify({'delete-field-1': 1, 'delete-field-2': 2}),
credentials: "include"
}).then((data) => data.json()).then((data) => console.log(data));
}
function del_header(url = '/delete') {
fetch(HOST + parser(url), {
method: "DELETE",
body: JSON.stringify({'delete-field-1': 1, 'delete-field-2': 2}),
headers: {
'X-ALLOWED-1': 'allowed-1',
"Content-Type": "application/text",
},
credentials: "include"
}).then((data) => data.json()).then((data) => console.log(data));
}
// post()
// post('/post-method')
// cpost()
// post_header()
// User SERVER TERMINAL to view the passed values
#!/usr/bin/env python
# Reflects the requests from HTTP methods GET, POST, PUT, and DELETE
# Written by Nathan Hamiel (2010)
# Cloned & Updated from: https://gist.github.com/1kastner/e083f9e813c0464e6a2ec8910553e632
import json
from http.server import HTTPServer, BaseHTTPRequestHandler
from optparse import OptionParser
allow = False
port = 8080
class RequestHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
pass
def process_request(self):
request_path = self.path
method = self.command.upper()
request_headers = self.headers
content_length = request_headers.get('Content-Length')
length = int(content_length) if content_length else 0
data = {
'path': request_path,
'method': method,
'content-length': content_length,
'headers': dict(request_headers),
'data': json.loads(str(self.rfile.read(length).decode('ascii')) or '{}')
}
print(json.dumps(data, indent=2))
self.send_response(200)
self.send_header("Content-Type", "application/json")
if allow:
self.send_header('ACCESS-CONTROL-ALLOW-CREDENTIALS', 'true')
self.send_header('ACCESS-CONTROL-ALLOW-HEADERS',
'Authorization, X-ALLOWED-1, '
'X-ALLOWED-2, X-ALLOWED-3, Content-Type') # * or Comma separated values
self.send_header('ACCESS-CONTROL-ALLOW-METHODS', 'DELETE, PATCH, POST, GET') # * or Comma separated values
self.send_header('ACCESS-CONTROL-ALLOW-ORIGIN', 'chrome-search://local-ntp') # * or Comma separated values
self.send_header('ACCESS-CONTROL-EXPOSE-HEADERS', '*') # * or Comma separated values
self.send_header('Set-Cookie', 'id=a3fWa; Expires=Wed, 21 Oct 2021 07:28:00 GMT')
# self.send_header('ACCESS-CONTROL-MAX-AGE', 600) # seconds
self.end_headers()
self.wfile.write(bytes(json.dumps({
'error': False,
'message': 'Handled ' + method + ' request',
}), 'utf-8'))
def serve_get(self):
request_path = self.path
if request_path == '/favicon.ico':
# Don't serve favicon
self.send_response(200)
self.end_headers()
return
self.process_request()
def serve_post(self):
self.process_request()
def serve_options(self):
self.process_request()
do_GET = serve_get
do_HEAD = serve_get
do_DELETE = serve_post
do_POST = serve_post
do_PUT = serve_post
do_PATCH = serve_post
do_OPTIONS = serve_options
def main():
print('Listening on 0.0.0.0:%s - %s CORS' % (port, 'ALLOWS' if allow else 'DISALLOWS'))
server = HTTPServer(('', port), RequestHandler)
server.serve_forever()
if __name__ == "__main__":
parser = OptionParser()
parser.usage = "Creates an http-server that will echo out HEAD, GET, POST, PUT, PATCH, DELETE in JSON format"
parser.add_option('-a', '--allow', default='no')
parser.add_option('-p', '--port', default=8080)
(options, args) = parser.parse_args()
if options.allow.lower()[0] == 'y':
allow = True
port = int(options.port)
main()
# Commands to run
# python server.py --port 8090
# python server.py --allow y
# python server.py --allow y --port 8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment