F
F
Fahrenhei72015-11-02 19:30:33
Ruby on Rails
Fahrenhei7, 2015-11-02 19:30:33

How to send a GET or POST request from a Rails controller file?

Hello. There is an application on rails and an SMS server, in the api of which it is written that in order to send SMS, you need to send a GET or POST request to the address " http://service.ru/sys/send.php?login=login&psw=paro
... " I have absolutely working code:

class OrdersController < ApplicationController
  def mainp
  end
  def succsess
  end
  def list
    @order = Order.all
  end
 def new
    @order=Order.new
  end
  def create
    @order = Order.new(order_params)
    if @order.save
# GET or POST request here!
      OrderMailer.new_call(@order).deliver
      flash[:success] = "Спасибо!"
      redirect_to succsess_path
    else
#
    end
  end
  def order_params
    params.require(:order).permit(:name, :phone)
  end
end

what should be inserted into the corresponding comment in the code in the line in order to send this request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima, 2015-11-02
@Fahrenhei7

require 'uri' # подключение библиотеки для работы с адресами возможно в rails это не нужно
uri = URI.parse('http://сервис.ru/sys/send.php') 
params = { login: 'login', password: '12345678' } # хэш с параметрами запроса
uri.query = URI.encode_www_form( params ) # кодирование параметров в строку запроса
require 'net/http' # подключение библиотеки для отправки HTTP запросов
Net::HTTP.get(uri) # собственно запрос

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question