mardi 25 avril 2023

HTTPS request for ruby

I would like to take information from another website. Therefore (maybe) I should make a request to that website (in my case a HTTP GET request) and receive the response.

How can I make this in Ruby on Rails?

If it is possible, is it a correct approach to use in my controllers?

if another website is http://www.test.com, it OK but https://www.test.com,

i got it SyntaxError ((eval):1: syntax error, unexpected '<'

^ (eval):2: syntax error, unexpected tCONSTANT, expecting end-of-input 404 Not Found ^~~): app/models/cart_payment.rb:155:in `eval'

SyntaxError ((eval):1: syntax error, unexpected '<'

^ (eval):2: syntax error, unexpected tCONSTANT, expecting end-of-input 404 Not Found ^~~): app/models/cart_payment.rb:155:in `eval'

url = URI.parse("https://test.com")

  http = Net::HTTP.new(url.host, url.port);
  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = "application/json"

  request.body = JSON.dump(body)

  response = http.request(request)

  response_body = eval(response.read_body)

  message = response_body[:Response]

Aucun commentaire:

Enregistrer un commentaire