You can use the Ruby standard library webrick
to create an HTTP server. To read the request body as JSON and return a JSON response, you need to include the json
gem in your program. Here's an example code snippet:
main.rb486 chars22 lines
This code starts an HTTP server on port 8000 and mounts a handler for the root URL. If the request method is POST
, it reads the request body as JSON using JSON.parse
, creates a JSON response with a message that includes the input, and sets the response status to 200. If the request method is not POST
, it sets the response status to 405 and includes an Allow
header to indicate that only POST
requests are allowed.
You can test this server using curl
or any other HTTP client that can send JSON requests.
main.rb187 chars3 lines
gistlibby LogSnag