I am trying to make a post request from my controller to a third party website using faraday but I can't get it to work?
I have a method in my homes controller to make the request, a route setup for the method and a form for my request but I can't get the request to be successful no idea what i am doing wrong
#homes_controller.rb
def alpaca
@conn = Faraday.post('https://paper-api.alpaca.markets/v2/orders') do |req|
req.headers['APCA-API-KEY-ID'] = 'key'
req.headers['APCA-API-SECRET-KEY'] = 'secret'
req.body = "{'symbol': '#{params[:ticker]}'}"
req.body = "{'qty': '#{params[:qty]}'}"
req.body = "{'side': '#{params[:side]}'}"
req.body = "{'type': '#{params[:ticker]}'}"
req.body = "{'time_in_force': '#{params[:time_in_force]}'}"
end
redirect_to homes_path
end
#routes.rb
resources :homes
root 'homes#index'
post '/alpaca', to: 'homes#alpaca'
#homes/index.html.rb
<%=form_tag alpaca_path do %>
<%=label_tag "Ticker"%>
<%=text_field_tag :ticker %>
<%=label_tag "qty"%>
<%=number_field_tag :qty %>
<%=label_tag "side"%>
<%=text_field_tag :side %>
<%=label_tag "type"%>
<%=text_field_tag :type %>
<%=label_tag "time_in_force"%>
<%=text_field_tag :time_in_force %>
<%=submit_tag%>
<%end%>
I am trying to get this form to post the params passed in sent to alpaca's trading api
Aucun commentaire:
Enregistrer un commentaire