Writing a curl Post call in Ruby - ruby-on-rails

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 '/'.

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.

Get cURL response body in Rails console

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

Convert the following CURL PUT to test the JSON request in rails

How do I convert the following CURL PUT request to rspec and test the behaviour of JSON parser.
curl -D - -X PUT --data "not json" -H "Content-Type: application/json" http://localhost:3000
And how to check the response? Thank you in advance.
The best is to use vcr gem https://github.com/vcr/vcr
Simply create script like in https://github.com/vcr/vcr#usage but with proper HTTP request.
VCR.use_cassette("json_test") do
req = Net::HTTP::Put.new('http://localhost:3000', 'not json')
req.content_type = 'application/json'
res = Net::HTTP.get_response(req)
end
With this real HTTP request will be recorded once and in rspec local file will be used instead.
You can use the -i option:
-i, --include
(HTTP) Include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more...
from http://curl.haxx.se/docs/manpage.html

Resources