问题
Is there a way to get POST data from G-WAN Ruby?
I've tried:
ENV.each do |k,v|
puts "#{k} => #{v} <br/>"
end
exit(200)
and test it using:
curl -d 'test2=1' 'http://127.0.0.1:8080/?test.rb&test=1' | gunzip -
but there are no post data (test2) shown:
GATEWAY_INTERFACE => CGI/1.1 <br/>
CONTENT_TYPE => urlencoded <br/>
REMOTE_HOST => 127.0.0.1 <br/>
USER => lab-hci-48 <br/>
REMOTE_ADDR => 127.0.0.1 <br/>
QUERY_STRING => test.rb&test=1 <br/>
CONTENT_LENGTH => 7 <br/>
PATH_TRANSLATED => /home/lab-hci-48/gwan/0.0.0.0_8080/#0.0.0.0/csp <br/>
REQUEST_URI => POST /?test.rb&test=1 <br/>
SERVER_SOFTWARE => G-WAN <br/>
PATH => /home/lab-hci-48/.rvm/gems/ruby-2.1.1/bin:/home/lab-hci-48/.rvm/gems/ruby-2.1.1@global/bin:/home/lab-hci-48/.rvm/rubies/ruby-2.1.1/bin:/home/lab-hci-48/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games <br/>
LANG => en_US.UTF8 <br/>
SERVER_PROTOCOL => HTTP/1.1 <br/>
PATH_INFO => / <br/>
SHELL => /bin/bash <br/>
REQUEST_METHOD => POST <br/>
PWD => /home/lab-hci-48/gwan <br/>
SERVER_PORT => 8080 <br/>
SCRIPT_NAME => test.rb <br/>
SERVER_NAME => 0.0.0.0 <br/>
Is there any other variable that I should check to get the POST data?
回答1:
In my ruby script i use this code to get data:
# Get arguments
k = ""
@params = {}
ARGV.each do|a|
if a.split("=").size > 1
a = a.split("=")
@params[a[0]] = a[1]
else
if k == ""
k = a
else
@params[k] = a
k = ""
end
end
end
It will create a params variable just like in Rails ;)
回答2:
Is there any other variable that I should check to get the POST data?
When the REQUEST_METHOD
is HTTP_POST
, both CONTENT_LENGTH
and CONTENT_TYPE
will let you find the attributes of the POST
entity (same story with HTTP_PUT
).
The X_HTTP_HEADERS
value (added last year) makes it easier and faster to find the end of the HTTP headers (where the POST
entity resides) - but finding the end of the HTTP headers (a double CRLF
) can be done in a Ruby script by starting from the REQUEST_URI
or the QUERY_STRING
.
The /csp/hello.rb
G-WAN example shows how to list query parameters:
# list QUERY PARAMETERS, if any:
#
# GET http://10.10.200.80/?hello.rb&arg1=123&arg2=456
arg: arg1=123
arg: arg2=456
ARGV.each do|a|
puts "arg: #{a}<br>"
end
来源:https://stackoverflow.com/questions/23507351/getting-posted-data-from-g-wan-ruby