Luiz Henrique
Luiz Henrique

Reputation: 957

Posting to other website's form and getting response with Rails

I am trying to send some params to this website (http://www.degraeve.com/translator.php) and get the response to my rails application. I want to select 'binary' from the radio buttons whose name is 'd' and put just 'a' on the text field whose name is 'w' to be translated. I am using this action on my controller:

class RoomsController < ApplicationController
  require "uri"
  require "net/http"
  require 'json'


  def test    

    uri = URI.parse("http://www.degraeve.com/translator.php")
    header = {'Content-Type': 'text/json'}
    params = { d: 'binary', w: 'a' }

    # Create the HTTP objects
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Post.new(uri.request_uri, header)
    request.body = params.to_json

    # Send the request
    response = http.request(request)
    render json: response.body
  end
end

Is there something wrong? It just renders the body of http://www.degraeve.com/translator.php before submitting the form, but I would like to get the body after it has been submitted.

Upvotes: 0

Views: 287

Answers (1)

xlts
xlts

Reputation: 136

When you look at what happens after you press the "Translate!" button you may notice that there is no form being submitted via POST. Instead, a GET request is sent and a HTML file is returned - see for yourself in your browser's network inspector.

Consequently, you can send a simple GET request with a prepared URL, like this (note the d and w query parameters):

uri = URI.parse("http://www.degraeve.com/cgi-bin/babel.cgi?d=binary&url=http%3A%2F%2Fwww.multivax.com%2Flast_question.html&w=a")
response = Net::HTTP.get_print(uri)

and then parse the response accordingly.

Upvotes: 1

Related Questions