setting query params to RESTful API with getURL in RCurl - r

I would like to translate the following HTTP GET command to work with RCurl:
curl -G https://api.example.com/resource \
-d "param=value" \
-d "param=value" \
-u 'user:password'
Here is my attempt using getURL in RCurl:
getURL("https://api.example.com/resource",
userpwd ="username:password",param="value",param="value")
The first code block works fine in my command line terminal and I have no troubles using getURL until I try to set parameters; I get warning messages saying that the params are "Unrecognized CURL options". Any ideas?

Anything going to the ... argument is interpreted as a curl option. You need to put parameters as a list in the httpheader argument. See documentation.
Try something like:
getURL("https://api.example.com/resource",
userpwd ="username:password",
httpheader=list(param1="value",param2="value"))

Related

Having trouble with GET method Clockify API

I am currently trying to use Clockify's API and when I input the following line in my terminal:
curl -H 'content-type':'application/json' -H 'X-Api-Key':'UserKey' -X GET https://api.clockify.me/api/workspaces/workspaceid/projects
I get the following message:
{"timestamp":"2019-01-08T17:24:47.011+0000","status":404,"error":"Not
Found","message":"No message
available","path":"/workspaces/workspaceid/projects"}
It seems like you're missing the forward dash at the end of the command. Just add / at the end of the URL and everything should work fine.
https://api.clockify.me/api/workspaces/workspaceid/projects/

Using meteor HTTP to connect to ethereum node

I am trying to copy the following curl command using meteor HTTP
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}' http://localhost:8545
The command is from here
This is what I tried:
HTTP.call('POST',"http://localhost:8545",{data:{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}},function(res,error){console.log(res)})
but it returns null. The curl command gives {"jsonrpc":"2.0","id":83,"result":"(the blocknumber)"}
You have to swap error and res params.
HTTP.call('POST',"http://localhost:8545",{data:{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":83}},function(error, res){console.log(res)})

curl ignore --data starting with # sign: don't read from file

In slack you can script slackbot to post messages to a channel like this:
curl --data "$msg" $'https://<yourteam>.slack.com/services/hooks/slackbot?token=<yourtoken>&channel=#random'
Now i'd like to mention a username as the first part of the message like msg="#joernhees hello self".
The problem with this is that if the --data argument of curl starts with an # sign it will interpret the string after the # as filename and post its content. Is there a way to make curl ignore the # sign and to send a literal # as the first char of a post request?
If you are on a new version of cURL you can also use the --data-raw option:
http://curl.haxx.se/docs/manpage.html#--data-raw
A word of warning is that looking my laptop it appears Yosemite ships with an older version of cURL.
In general if you're creating tools to post to Slack I'd recommend using an HTTP library in your script rather than calling out to a shell and invoking the curl command.
Actually i just found out i can do this (not sure it's the best option though):
curl --data '#-' $'https://<yourteam>.slack.com/services/hooks/slackbot?token=<yourtoken>&channel=#random' <<< "$msg"
The trick is to tell curl to read from stdin #- and then pass the message in via that.

CURL Command Line URL Parameters

I am trying to send a DELETE request with a url parameter using CURL. I am doing:
curl -H application/x-www-form-urlencoded -X DELETE http://localhost:5000/locations` -d 'id=3'
However, the server is not seeing the parameter id = 3. I tried using some GUI application and when I pass the url as: http://localhost:5000/locations?id=3, it works. I really would rather use CURL rather than this GUI application. Can anyone please point out what I'm doing wrong?
The application/x-www-form-urlencoded Content-type header is not required (well, kinda depends). Unless the request handler expects parameters coming from the form body. Try it out:
curl -X DELETE "http://localhost:5000/locations?id=3"
or
curl -X GET "http://localhost:5000/locations?id=3"
#Felipsmartins is correct.
It is worth mentioning that it is because you cannot really use the -d/--data option if this is not a POST request. But this is still possible if you use the -G option.
Which means you can do this:
curl -X DELETE -G 'http://localhost:5000/locations' -d 'id=3'
Here it is a bit silly but when you are on the command line and you have a lot of parameters, it is a lot tidier.
I am saying this because cURL commands are usually quite long, so it is worth making it on more than one line escaping the line breaks.
curl -X DELETE -G \
'http://localhost:5000/locations' \
-d id=3 \
-d name=Mario \
-d surname=Bros
This is obviously a lot more comfortable if you use zsh. I mean when you need to re-edit the previous command because zsh lets you go line by line. (just saying)

HTTP request in Ubuntu

i need to call a HTTP request in ubuntu how do i do it? I can't seem to find an answer around on how to to do it?
How do run the following url without calling a browser like lynx to do it?
http://www.smsggglobal.com/http-api.php?action=sendsms&user=asda&password=123123&&from=123123&to=1232&text=adsdad
in your command prompt, run the following:
curl http://www.smsggglobal.com/http-api.php?action=sendsms&user=asda&password=123123&&from=123123&to=1232&text=adsdad
the curl command executes an http request for a given url and parameters.
if you need to specify another HTTP method, use curl -X <TYPE> <URL>, like this:
curl -X POST http://www.smsggglobal.com/http-api.php?action=sendsms&user=asda&password=123123&&from=123123&to=1232&text=adsdad
curl documentation: http://curl.haxx.se/docs/manpage.html
to display the results:
curl http://www.smsggglobal.com/http-api.php?action=sendsms&user=asda&password=123123&&from=123123&to=1232&text=adsdad
or
to save the results as a file
wget http://www.smsggglobal.com/http-api.php?action=sendsms&user=asda&password=123123&&from=123123&to=1232&text=adsdad

Resources