ruby - How to get the content from an HTTP POST request received in thin? -
i using thin receive http post requests, server code this:
http_server = proc |env| # want make response dependent on content response = "hello world!" [200, {"connection" => "close", "content-length" => response.bytesize.to_s}, [response]] end
setting breakpoint, can see have received content-type (json), , content length, can't see actual content. how can retrieve content request processing?
you need use rack.input
entry of env
object. rack spec:
the input stream io-like object contains raw http post data. when applicable, external encoding must “ascii-8bit” , must opened in binary mode, ruby 1.9 compatibility. input stream must respond
gets
,each
,read
,rewind
.
so can call read
on this:
http_server = proc |env| json_string = env['rack.input'].read json_string.force_encoding 'utf-8' # since body has ascii-8bit encoding, # know json, can use # force_encoding right encoding # parse json_string , stuff response = "hello world!" [200, {"connection" => "close", "content-length" => response.bytesize.to_s}, [response]] end
Comments
Post a Comment