簡體   English   中英

在Rails中發送POST請求

[英]Sending POST request in Rails

如何在Rails設置中以下列格式發送HTTP POST請求:

curl https://api.venmo.com/v1/payments -d access_token=4e4sw1111111111t8an8dektggtcbb45 -d     email="someemail@gmail.com" -d amount=5 -d note="Delivery."

我希望用戶將電子郵件/金額/注釋參數輸入到網頁上的表單中,然后將該數據(當用戶單擊提交按鈕時)傳遞到存儲access_token參數的Controller中,然后觸發POST請求。

到目前為止,我已嘗試使用此方法設置Controller(並從視圖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>

我覺得我在這里接近解決方案......

您可以使用httpparty gem來實現它,它只在一行中易於使用:

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

如果您沒有特定的Body或Header,可以刪除Body和Header,

如果你想實現Get請求更容易:

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

您需要使用表單才能從網頁生成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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM