Get cURL response body in Rails console - ruby-on-rails

I'm working on a db migration where I need to upload images in the old db to Filepicker using cURL within the Rails console. I've got a command that works, and all I need from here is to grab the Filepicker URL from the response. However, setting the response as below just returns true.
So this will upload a file to Filepicker as desired:
res = system("curl -X POST -F fileUpload=#oldimages/oldimage#{id}.jpg https://www.filepicker.io/api/store/S3\?key\=OURAPIKEY")
Giving this output:
{"url": "https://www.filepicker.io/api/file/THEURL", "size": 207610, "type": "image/jpeg", "filename": "oldimage1.jpg"} => true
The problem is I can't assign the URL - the res variable only stores true.
So, in a nutshell, how to I get access to the rest of this data to assign it to an instance in the new db?

If you need run curl you can do this
body=`curl -X POST -F fileUpload=#oldimages/oldimage#{id}.jpg https://www.filepicker.io/api/store/S3\?key\=OURAPIKEY`
or
body = %x(curl -X POST -F fileUpload=#oldimages/oldimage#{id}.jpg https://www.filepicker.io/api/store/S3\?key\=OURAPIKEY)
or
IO.popen("curl -X POST -F fileUpload=#oldimages/oldimage#{id}.jpg https://www.filepicker.io/api/store/S3\?key\=OURAPIKEY").read
But If I were you, I would try to use net:http or another ruby http client as typhoeus or restclient

Related

RestClient syntax for file uploads

Following the indications provided by this gem , testing in the Rails console the RestClient
RestClient.post "https://upload.twitter.com/1.1/media/upload.json?media_category=tweet_image", :myfile => File.new("/Users/main/Desktop/ss2022-11-03_11.39.11.png", 'rb')
one can assert that the file exists by changing its path. Once verified, the response returns
400 Bad Request. So the request is malformed. Alas, I do not grasp what the second element of the File.new represents
The version 1.1 API documentation provides a suggestion console test
twurl -X POST -H upload.twitter.com "/1.1/media/upload.json?media_category=TWEET_IMAGE&additional_owners=3805104374" -f adsapi-heirarchy.png -F media
the response is the same whether the additional_owners is included or not (logical, it is optional).
What is missing in this sequence?
Thr translation of the twurl syntax to curl syntax would be:
curl "https://upload.twitter.com/1.1/media/upload.json?media_category=tweet_image&additional_owners=3805104374" --data-urlencode #/Users/main/Desktop/adsapi-heirarchy.png --data-urlencode 'media'

MALFORMED_REQUEST on PayPal execute payment

I've lost almost two whole days trying to solve PayPal related issues.
I'm trying to execute a payment after user's approval, but I'm getting MALFORMED_REQUEST everytime I send the Curl request.
def execute
# Get the access_token
token = get_paypal_token(false)
payer_id = params[:PayerID]
payment_id = params[:paymentId]
# Creating the data object
stringJson = {:payer_id => "#{payer_id}"}
# You can see that I hard-coded the payer_id to ensure the JSON is correct
curlString = `curl -v -H "Authorization: Bearer #{token}" -H "Content-Type: application/json" -d '{"payer_id" : "QULQSFESGMCX2"}' https://api.sandbox.paypal.com/v1/payments/payment/#{payment_id}/execute/`
end
And the response is:
"{\"name\":\"MALFORMED_REQUEST\",\"message\":\"The request JSON is not well formed.\",\"information_link\":\"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST\",\"debug_id\":\"6431589715660\"}"
Looks like you are running it from windows. I tried your command from my windows and my test server receiving the json as '{payer_id. It is happening because of your single quote.
I have changed the single quotes into double and it is working fine here with me.
-d "{\"payer_id\" : \"QULQSFESGMCX2\"}"
You can run the curl command from the console and see what happens, after that you can push it inside your ruby script code.

why is this parse.com rest api request failing?

i'm trying to send a parse.com push notification from ruby 1.8.7.
i got a test working with curl. but with ruby's net::http i'm getting Timeout::Error: Resource temporarily unavailable
how can i debug this? i don't know how to see why the parse server is responding differently or otherwise see what's happening. i tried sending the request to my own server and the headers looked ok to me.
i simplified what i'm doing to this:
http = Net::HTTP.new('api.parse.com', 443)
response = http.post("/1/push", "{\"where\":{},\"data\":{\"alert\":\"Elliot net http json test 1\"}}", {"X-Parse-Application-Id"=>"xxxxx", "Content-Type"=>"application/json", "X-Parse-REST-API-Key"=>"xxxxx"})
the json there is hard to read, it's from:
api_req = {:where => {}, :data => {:alert => "Elliot net http json test 1"}}.to_json
puts api_req
# {"where":{},"data":{"alert":"Elliot net http json test 1"}}
i also tried several other ways of sending a request with net::http. same result.
the curl request that worked was:
curl -X POST \
-H "X-Parse-Application-Id: xxxxxx" \
-H "X-Parse-REST-API-Key: xxxxx" \
-H "Content-Type: application/json" \
-d '{
"where": {},
"data": {
"alert": "Elliot curl test #4"
}
}' \
https://api.parse.com/1/push
i'm not using parse-ruby-client because i ran into problems with dependencies assuming a newer version of ruby. all i need to do is send some simple push notifications, and it seems like this should work without too much trouble.
can anyone help me get this working or tell me how to get some useful info about what's happening to debug?
As per the REST API Developers guide,
All API access is over HTTPS, and accessed via the https://api.parse.com domain.
So all you need to do is to add http.use_ssl = true.

How to read file data from CURL param?

In my sinatra application, I am using following curl command to post a file in route /test.
curl -H "Content-Type: text/xml" -vv -X POST -d file_data=#test.xml
http://localhost:4567/test
At my Sinatra post method I want read file like
post '/test' do
data = params[:file_data] #Here file_data param name of CURL command.
end
But, here data is NULL. How should I configure my CURL command to read file from file_data param ?
curl -F file_data=#/some/file/on/your/local/disk http://localhost:4567/test
from cURL manual:
-F, --form <name=content>
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an # sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between # and < is then that # makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.
for more details: cURL manual
Quick answer: Remove -H "Content-Type: text/xml" and it should work.
The problem is that when a file is sent, the Content-Type header should be multipart/form-data, which curl sets automatically when you use -F. From curls documentation:
-F, --form
(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit
button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files etc.
However, you are overwriting this header with -H, so your app is expecting something that is not a file.
If, in addition to sending the file with curl, you want to include information about its type, you should instead do this:
curl -X POST -F "file_data=#test.xml;type=text/xml" http://localhost:4567/test
(The content type comes after the file name and a semicolon.)
To see how a raw form submission request ends up looking like with multipart/form-data, check the answers to this question.

Writing a curl Post call in Ruby

If i issue a POST request using curl command to github api to add "Resolved" label; things are good.
curl -i -H "Authorization: token xxxxxxxxxxxx" -X POST 'https://git.corp.yahoo.com/api/v3/repos/owner/repo/issues/1/labels' -d '["Resolved"]'}
But when i try to do the same using Curl easy in my Ruby script
set gh_api = https://git.corp.yahoo.com/api/v3/repos/owner/repo/issues/1/labels
curl = Curl::Easy.http_post(settings.gh_api,'["Resolved"]')
do |c|
c.headers = ["Authorization: token xxxxxxxx"]
end
The JSON reponse I get is
"{\"message\":\"Not Found\"}"
What am i doing wrong in my ruby script?
It's not at all clear what question you are asking because there is no question.
What's returned is a JSON response. Why don't you parse it with the JSON class and see what it returns:
require 'json'
puts JSON["{\"message\":\"Not Found\"}"]
=> {"message"=>"Not Found"}
I'm guessing that the problem is in the path of the URL if the response is "Not Found".
My URL was wrong. I ended the URL with a '/'.

Resources