Skip to content

Instantly share code, notes, and snippets.

@yagays
Last active November 29, 2018 04:46
Show Gist options
  • Save yagays/bc148738014efc6d38a4744cf9778d44 to your computer and use it in GitHub Desktop.
Save yagays/bc148738014efc6d38a4744cf9778d44 to your computer and use it in GitHub Desktop.
import json
from http.server import HTTPServer, SimpleHTTPRequestHandler
class CustomHandler(SimpleHTTPRequestHandler):
def do_GET(self):
SimpleHTTPRequestHandler.do_GET(self)
print("===headers===\n", self.headers)
print("===path===\n", self.path)
def do_POST(self):
self.send_response(200)
self.send_header('Content-type', 'application/json; charset=UTF-8')
self.send_header('Content-length', 0)
self.end_headers()
content_len=int(self.headers.get('content-length'))
requestBody = json.loads(self.rfile.read(content_len))
print("===headers===\n", self.headers)
print("===body===\n", requestBody)
print("===path===\n", self.path)
httpd = HTTPServer(("localhost", 8080), CustomHandler)
httpd.serve_forever()
curl "http://localhost:8080/hoge/fuga"
curl -X POST -H "Content-Type: application/json" -d '{"name":"hoge", "text":"ほげふが"}' localhost:8080/hoge
127.0.0.1 - - [29/Nov/2018 13:40:49] "GET /hoge/fuga HTTP/1.1" 404 -
===headers===
Host: localhost:8080
User-Agent: curl/7.54.0
Accept: */*
===path===
/hoge/fuga
127.0.0.1 - - [29/Nov/2018 13:41:41] "POST /hoge HTTP/1.1" 200 -
===headers===
Host: localhost:8080
User-Agent: curl/7.54.0
Accept: */*
Content-Type: application/json
Content-Length: 38
===body===
{'name': 'hoge', 'text': 'ほげふが'}
===path===
/hoge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment