开发者

Parse body of POST reqest in self-made server in Ruby

I am writing mock Ruby servers to test components of API. I send a POST request with a body, and I'd like my mock server to return a body of that POST request. Currently i hav开发者_开发知识库e this code:

require 'socket'

webserver = TCPServer.new('127.0.0.1', 7125)
loop do
  session = webserver.accept
  session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
  request = session.gets
  session.puts request
  session.close
end

A POST request with body FOO returns a response with body that contains only POST / HTTP/1.1 How to fix it?


Writing your own HTTP server is really going to get you into trouble because the specification, while superficially simple, has a number of subtle nuances that can trip you up. In this case, you're reading one line with gets and ignoring the bulk of the submission. You're going to have to address that by reading in and properly decoding the posted data.

For something with a familiar interface you might start with Net::HTTP::Server instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜