Search code examples
ruby-on-railscurlgoogle-checkoutcurb

Trying to make curl requests in ruby


is there a ruby curl library that will allow me to duplicate this request:

curl -d '<hello xmlns="http://checkout.google.com/schema/2"/>' https://S_MERCHANT_ID:[email protected]/checkout/api/checkout/v2/request/Merchant/S_MERCHANT_ID

i have tried curb, but their PostField.content class is not cooperating with google's checkout api. here is the code from my curb request:

c = Curl::Easy.new("https://MY_ID:[email protected]/checkout/api/checkout/v2/request/Merchant/MY_ID_AGAIN")
c.http_auth_types = :basic
c.username = 'MY_ID'
c.password = 'MY_KEY'
# c.headers["data"] = '<?xml version="1.0" encoding="UTF-8"?><hello xmlns="http://checkout.google.com/schema/2"/>'
c.http_post(Curl::PostField.content('', '<?xml version="1.0" encoding="UTF-8"?><hello xmlns="http://checkout.google.com/schema/2"/>'))
c.perform

i HAVE managed to get it working using ruby's system command, but im not sure how to handle the response from it.

req = system("curl -d '<hello xmlns=\"http://checkout.google.com/schema/2\"/>' https://MY_ID:[email protected]/checkout/api/checkout/v2/request/Merchant/MY_ID")

I have been at it for 2 hours now. any help would be greatly appreciated, thanks!


Solution

  • I figured it out (YAAAAY!)

    if anyone else is having this problem, here is the solution.

    executable commands work fine in the command line, but if you are trying to render the output of an executable command from a controller in rails, make sure you use render :json instead of render :text to print the results.

    for some reason the render :text was only outputting bits and pieces of my command's output (and driving me insane in the process).

    For those of you trying to integrate with google checkout in rails, here is how you make http requests to google:

    First step: add rest-client to your Gemfile. here is how to do it from the command line:

    $ cd /path/to/your/rails/app
    $ sudo nano Gemfile
    

    Next, add the gem to your gemfile by placing the following somewhere in your Gemfile

    $ gem "rest-client"
    

    next, run bundle install

    $ bundle install
    

    restart your server. if apache2:

    $ sudo service apache2 reload
    

    if webrick:

    $ rails s
    

    then, in your controller (assuming you have rails set up and are able to access a controller from the browser) write the following code:

    $ url = "https://YOUR_GOOGLE_CHECKOUT_MERCHANT_ID:[email protected]/checkout/api/checkout/v2/request/Merchant/YOUR_GOOGLE_CHECKOUT_MERCHANT_ID"
    $ req = RestClient.post(url, '<hello xmlns="http://checkout.google.com/schema/2"/>')
    render :json => req
    

    Please don't forget to replace YOUR_GOOGLE_MERCHANT_ID with your actual merchant id and YOUR_GOOGLE_CHECKOUT_KEY with your actual google checkout key

    <?xml version="1.0" encoding="UTF-8"?>
    <bye xmlns="http://checkout.google.com/schema/2" serial-number="1dfc3b90-1fa6-47ea-a585-4d5482b6c785" />
    

    (answer courtesy of nexo)