Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7254

CGIHandler: how to handle post requests

Newsgroups comp.lang.ruby
Date 2016-05-23 09:41 -0700
Message-ID <63fbfa4c-2b10-46b5-9061-6e03a065af0f@googlegroups.com> (permalink)
Subject CGIHandler: how to handle post requests
From hypnotortoise@gmail.com

Show all headers | View raw


I'm writing this simple CGI handler for an endpoint directly in the webserver using the ruby cgi library. So, I want to respond with 405 when it's not a POST, and 200 otherwise.

# upload.cgi
require 'cgi'

cgi = CGI.new

if cgi.request_method != "POST"                                    
 cgi.out("type" => "text/plain",                                   
         "connection" => "close",                                  
         "status" =>"METHOD_NOT_ALLOWED") { "Method Not Allowed" } 
                                                                   
else                                                               
   cgi.out { "hurray" }
end

# server.rb
server = WEBrick::HTTPServer.new :Port => ENV["PORT"] || 8989                
server.mount "/upload", WEBrick::HTTPServlet::CGIHandler , './upload.cgi' 
trap('INT') { server.stop }                                                  
server.start                                                                 

the server.rb is only to test it locally. I'm using curl to test it. So, when I do:

> curl http://localhost:8989/upload -I
 => HTTP/1.1 405 Method Not Allowed 
 ....

But when I do:

> curl -X POST http://localhost:8989/upload -I
  => HTTP/1.1 411 Length Required 
 ....

It was my understanding according to the docs that the length would be filled automatically, and if the cgi handles GET requests, in fact it does. So I assume this is a POST "problem". How can I respond successfully?

Back to comp.lang.ruby | Previous | Next | Find similar


Thread

CGIHandler: how to handle post requests hypnotortoise@gmail.com - 2016-05-23 09:41 -0700

csiph-web