Reputation: 21
I need to make a simple API using Python.
There are many tutorials to make a REST API using Django REST framework, but I don't need REST service, I just need to be able to process POST requests.
How can I do that? I'm new to Python.
Thank you!
Upvotes: 0
Views: 6249
Reputation: 1592
You can use HTTPServer module alongwith SimpleHTTPRequestHandler to create a simple webserver that serves your GET and POST request
from http.server import BaseHTTPRequestHandler,HTTPServer, SimpleHTTPRequestHandler
class GetHandler(SimpleHTTPRequestHandler):
def do_GET(self):
SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = b'<html><body><h1>POST!</h1></body></html>'
self.wfile.write(bytes(data))
return
Handler=GetHandler
httpd=HTTPServer(("localhost", 8080), Handler)
httpd.serve_forever()
Upvotes: 6
Reputation: 77912
Well if you don't need the whole DRF stuff than just don't use it.
Django is built around views which take HTTP requests (whatever the verb - POST, GET etc) and return HTTP responses (which can be html, json, text, csv, binary data, whatever), and are mapped to urls, so all you have to do is to write your views and map them to url.
Upvotes: 0