Mill3r Mill3r - 1 month ago 8
Ruby Question

How to POST a message into a site

I was trying to post a message into a test site with the help of the command

ruby test.rb websiteLink /message POST "***the message***"


I could do it if I write the message in the Ruby code. However I want to find a way to pass a string variable which will enable the user to input the message while giving the above command.

My post code in the test.rb file looks something like this:

def Ruby_test.post_method (host, path)
require 'net/http'

#get the host url: the host parameter is accessed by the command line
uri = URI.parse(host)
#refers to the module the class in is
http=Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Post.new("/messages")
request.set_form_data(:message => "***My Message***")
reply=http.request(request)

end


What is the correct way to pass in a variable which will allow the user to do that?

Answer

User input from command line is stored in the ARGV array.

So,

request = Net::HTTP::Post.new(ARGV[1])    
request.set_form_data(:message => ARGV[3])