2014-05-09 223 views
0

我怎么会去在Rails设置发送以下列格式的HTTP POST请求:发送Rails中POST请求

curl https://api.venmo.com/v1/payments -d access_token=4e4sw1111111111t8an8dektggtcbb45 -d  email="[email protected]" -d amount=5 -d note="Delivery." 

我想有用户输入的电子邮件/数量/注参数成网页上的表单,然后将该数据(当用户单击提交按钮时)传递到存储access_token参数的Controller,然后触发POST请求。

到目前为止,我已经尝试设置控制器用这种方法(从视图html.erb调用它):

def make_payment 
    if !params[:access_token].nil? 

    form_data = { 
     "email" => @email_to, 
     "amount" => @amount_to, 
     "note" => @note_to, 
     "access_token" => :access_token 
    } 
    url = "https://api.venmo.com/v1/payments" 
    request = Net::HTTP::Post.new(url, form_data) 

    response = http.request(request) 

    end 
end 

这是我当前视图设置:

<div class="box box-warning"> 
         <div class="box-header"> 
          <h3 class="box-title">Pay Bill using Venmo Account</h3> 
          <div id="payment_note_input"> 
          <input id="payment_note" type="text" class="form-control" required placeholder="add a note to your payment"> 
          </div> 
          <div id="payment_target_input" > 
          <input id="payment_target" type="text" class="form-control" required placeholder="pay an email address"> 
          </div> 
          <div id="payment_amount_input"> 
          <input id="payment_amount" type="text" class="form-control" required placeholder="enter the payment amount! "> 
          </div> 
          </br> 
         <button class="btn btn-danger" type="button" onClick= <%=make_payment%> > Make payment</button> 
        </div> 

我觉得我在这里接近解决方案...

回答

0

您需要使用表单才能从网页生成POST请求。 Rails为您提供可帮助您实现此目的的表单助手。

<div class="box box-warning"> 
    <div class="box-header"> 
    <%= form_tag make_payment_path do %> 
     <%= hidden_field_tag "access_token", @access_token %> 
     <h3 class="box-title">Pay Bill using Venmo Account</h3> 
     <div id="payment_note_input"> 
     <%= text_field_tag "payment_note", nil, class: "form-control", placeholder: "add a note to your payment" %> 
     </div> 
     <div id="payment_target_input" > 
     <%= text_field_tag "payment_target", nil, class: "form-control", placeholder: "pay an email address" %> 
     </div> 
     <div id="payment_amount_input"> 
     <%= text_field_tag "payment_amount",nil, class:"form-control", placeholder: "enter the payment amount! "> 
     </div> 
     </br> 
     <%= sumbit_tag "Make payment", class:"btn btn-danger" %> 
     <% end %> 
    </div> 

然后你就可以通过...

def make_payment 
    access_token = params[:access_token] 
    note = params[:payment_note] 
    target = params[:payment_target] 

    ... 
end 
+0

我怎么会那么去在我的控制器在方法中引用它们? Params [:payment_note]? – blunatic

+0

是的......但Params是一个变量,所以它应该以小写字母P开头。 –

2

访问控制器表单变量您可以使用httpparty gem实现这一目标,它很容易在只有1号线使用方法:

response = HTTParty.post("https://example.com?param1=value1", 
       :body => {:text => data}.to_json, 
       :headers => {'Content-Type' => 'application/json'} 
) 

如果您没有特定的正文或标题,您可以移除正文和标题,

,如果你想实现GET请求其更容易:

responce = HTTParty.get('http://example.com.json') 
json=JSON.parse(responce.body) # in case you expect json responce