How to write function in .zsh profile that returns cURL response? - zsh

At work, I run a command daily to return a specific token for access to our local repos. It's not a big deal, but I was trying to figure out a faster way of generating this token. Usually, I paste it into VSCode and run a Send Request via the REST Client Extension.
I'm trying to create a function in my .zsh profile that can then be run in my command line instead. Here's what I currently have:
generateToken() {
response=curl -H "Content-Type: application/json" -H "service-token: xxxx" -X POST -d '{"user": "me.myself#xxxxxxx.com"}' <the-url.com>
echo "$(response)"
}
Then I save, quit, and run eval ${generateToken}, but nothing happens. Any suggestions on the best way to get this going?

Thanks to #thatotherguy in the comments, I was able to create the following:
function generateToken() {
echo "Generating token..."
response=$(curl -H "Content-Type: application/json" -H "service-token: xxxxxxx" -X POST -d '{"user": "me.myself#mycompany.com"}' https://api.my.company/token)
echo "${response}"
}

If you're not doing anything to the output, you don't need to capture it and echo it. You can just run the command. It will write its output to the same place that echo would if you captured and re-echoed the output.
generateToken() {
echo "Generating token..."
curl -H "Content-Type: application/json" -H "service-token: xxxxxxx" \
-X POST -d '{"user": "me.myself#mycompany.com"}' \
https://api.my.company/token
}

Related

curl upload file -T

I get the error curl: Can't open 'files=#1.txt'! when trying to run code:
curl -v -XPOST -k -H "Accept: application/json" -T "files=#1.txt" https://192.168.1.102/
any suggestion on how to pass the text file's name properly?
-T is for PUT and wants a file name only:
curl -T 1.txt https://192.168.1.102/
You seem to want to POST a file? If you want it sent "plainly", you probably want:
curl -H "Accept: application/json" --data-binary #1.txt https://192.168.1.102/
If you want to instead send the file as a multipart formpost, you might do it similar to:
curl -F files=#1.txt https://192.168.1.102/

How can I use the Pingdom API to pause and resume checks from bash?

I'm writing a quick and dirty deployment script and would like to disable and reenable a Pingdom check as part of it. How do I do that using something like cURL?
To pause a check:
curl -X PUT -u 'your#email:yourpassword' -H 'Content-Type: application/json' -H 'App-Key: yourapplicationkey' -d 'paused=true' https://api.pingdom.com/api/2.0/checks/checkid
To resume a check:
curl -X PUT -u 'your#email:yourpassword' -H 'Content-Type: application/json' -H 'App-Key: yourapplicationkey' -d 'paused=false' https://api.pingdom.com/api/2.0/checks/checkid
Replace your#email with your pingdom email.
Replace yourpassword with your pingdom password.
Replace yourapplicationkey with a generated key from the "Sharing" section in your account.
Replace checkid with the numeric ID you see in the browser URL when you click on your check in the Pingdom UI.
You can also use modern way - just API key instead of using also email/password.
First, generate your own API key in https://my.pingdom.com/app/api-tokens and then you can use curl commands like for pausing:
curl -X PUT \
https://api.pingdom.com/api/3.1/checks \
-H 'Authorization:Bearer YOURAPIKEY' \
-d 'paused=true&checkids=777'
or for resuming:
curl -X PUT \
https://api.pingdom.com/api/3.1/checks \
-H 'Authorization:Bearer YOURAPIKEY' \
-d 'paused=false&checkids=777'
Replace YOURAPIKEY with your real API key and 777 with valid check ID.
checkids can be also omitted, then all checks will be modified.

how to post a json file with documents to couchdb

I am trying to post some documents to couchdb by curl and I have succeeded by choosing local file but not http-url... I have trying something like this:
curl -d #http://111.111.11.1/json/myjsonfile -X POST http://127.0.0.1:5984/MyTestDb/_bulk_docs -H "Content-Type: application/json"
I have been trying with many flags and tried many ways but I thing I am missing something. Is there anyone who can help?
The -d option for curl expects a local file only. You'll have to download it first. You could try piping the output of a curl download to a PUT to your CouchDB:
curl http://111.111.11.1/json/myjsonfile | curl -d #- -X PUT http://localhost:5984/MyTestDb....

FireBase, NEST. I can GET, but cannot PATCH using curl. Why am I getting a 400 error?

I am using the url at the developer-api.nest.com site, and my request is re-directed to the firebase-apiserver01...01.dapi.production.nest.com
I get the correct structured data back, using this dos command:
curl -v -k -L -X GET "https://developer-api.nest.com/structures/Za6hCZpmt4g6mBTaaA96yuY87lzLtsucYjbxW_b_thAuJJ7oUOelKA/?auth=c.om2...AeiE"
I get the error 'Invalid content sent' when I send this PATCH
curl -v -k -L -X PATCH "https://developer-api.nest.com/structures/Za6hCZpmt4g6mBTaaA96yuY87lzLtsucYjbxW_b_thAuJJ7oUOelKA/?auth=c.om2...AeiE" -H "Content-Type: application/json" -d '{"away":"home"}'
I have tried adding '.json' before the question mark, but get the same error.
To set the structure to home/away you'll need to send a PUT request for example as follows:
curl -v -L -X PUT "https://developer-api.nest.com/structures/g-9y-2xkHpBh1MGkVaqXOGJiKOB9MkoW1hhYyQk2vAunCK8a731jbg?auth=<AUTH_TOKEN>" -H "Content-Type: application/json" -d '{"away":"home"}'
Hope that helps
--Nagesh

Is it possible to use `--data-urlencode` and `--data-binary` options for the same curl command?

I am using curl and I would like to execute a HTTP PUT request by sending a --data-urlencode string and a --data-binary JSON file content. Is it possible to make that in the same curl command?
I tried the following
curl www.website.org --request PUT -H Content-Type: application/json --data-urlencode "key=sample_string" --data-binary #sample_file.json
but it seems do not work as expected: key=sample_string and sample_file.json content are not send at all.
A couple of things here;
Your curl request is missing double quotes for the headers. It should rather be:
curl www.website.org --request PUT -H "Content-Type: application/json" \
--data-urlencode "key=sample_string" --data-binary #sample_file.json
Your content-type is application/json which I hope is not "binary", so you should rather be using an appropriate type.
In any case, you should be able to find the submitted values using a simple php script as follows:
$putfp = fopen('php://input', 'r');
$putdata = '';
while($data = fread($putfp, 1024))
$putdata .= $data;
fclose($putfp);
var_dump($putdata);
echo "---CONTENT_TYPE---\n";
var_dump($_SERVER['CONTENT_TYPE']);

Resources