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:S_MERCHANT_KEY@sandbox.google.com/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:MY_KEY@sandbox.google.com/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:MY_KEY@sandbox.google.com/checkout/api/checkout/v2/request/Merchant/MY_ID")

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

有帮助吗?

解决方案 5

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:YOUR_GOOGLE_CHECKOUT_KEY@sandbox.google.com/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)

其他提示

You can use IO.popen to read from the child process:

IO.popen(['curl', '-o', '-', '-d', ..., err: [:child, :out]]) do |io|
  response = io.read
end

This example combines standard out and standard error into one stream in the child process, and it forces curl to redirect output to standard out via -o. You would specify your other options in place of the ....

I always use Rest Client gem for such use cases, it is very simple in use and have all REST requests out-of-box with whole batch of tuning parameters.

Your code will look like something similar to this:

url = "sandbox.google.com/checkout/api/checkout/v2/request/Merchant/#{S_MERCHANT_ID}"
credentials = "#{S_MERCHANT_ID}:#{S_MERCHANT_KEY}"
RestClient.post "https://credentials@#{url}", '<hello xmlns="http://checkout.google.com/schema/2"/>' 

Alternatively, you can use a HTTP request library such as Typheous (https://github.com/typhoeus/typhoeus). Is there anything that binds you with "curl"?

I would have curl put the result in a file, and then open the file using ruby and read it ( File.open)

Or us httparty

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top