简体   繁体   中英

Sending POST request in Rails

How would I go about sending an HTTP POST request in the following format in a Rails setup:

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

I'd like to have users input the email/amount/note parameters into a form on a webpage, then pass that data (when the user clicks the submit button) into a Controller where the access_token parameter is stored to then fire off the POST request.

So far I've tried setting up the Controller with this method (and calling it from the view 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

Here's my current view setup:

  <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>

I feel like I'm close to a solution here...

You can use httpparty gem to achieve that, its easy to use in only 1 line:

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

You can remove Body and Header if you don't have specific Body or Header,

and if you want to achieve Get request its even easier:

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

You need to use a form in order to generate a POST request from the web page. Rails provides you with Form helpers that would help you achieve this.

<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>

And then you can access the form variables in your controller by...

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

  ...
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM