Grep Curl command's console output to a file - unix

Need an help in doing grep of 3 variables from the curl commands console output.
By executing the below curl command i get some output which prints in the console itself. I need to grep some variables(say staus, name, url) and redirect it to a file.
curl -v -X POST -D tmp.txt -H "Content-Type:text/plain" --data "$SECRET" -H "Accept:application/xml" -H "Connection:close" http://google.com/api/search

Some of your responses must be going to stderr, try:
curl ... | grep 'pattern' &> filename

I'd recommending using slightly different options to curl if you want to process the output. If I were looking for the Expires header:
curl -si -X POST -H "Content-Type:text/plain" --data "$SECRET" -H "Accept:application/xml" -H "Connection:close" http://google.com/api/search
If you want the HTTP status, you can just do this:
curl -si -X POST -H "Content-Type:text/plain" --data "$SECRET" -H "Accept:application/xml" -H "Connection:close" http://google.com/api/search | head -1
That'll print HTTP/1.1 301 Moved Permanently -- add an |awk '{print $2}' to the end of that and you'll get only the numeric status.

Related

Using grep to remove from verbose call and saving in a shell script function

I have a curl put request that I can use some unix command to see the stdout and stderr from.
If I add the line 2>&1 | grep -v "Authorization" At the end of my curl request, I can see the verbose output from my curl in my CLI minus a line that starts with "Authorization"
I tried creating a function with the above command, but when I call that function at the end of my curl request (like below) it no longer removes the "Authorization" line.
function remove_item_from_verbose {
2>&1 | grep -v "Authorization"
}
echo -e "\n +++ Creating '$TENANT' tenant:\n"
# Create tenant
curl -L -X PUT "http://localhost:$HOST_PORT/admin/v2/tenants/$TENANT" \
--verbose \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $AUTHORIZATION" \
--data-raw "{\"allowedClusters\": [\"$CLUSTER\"]}" remove_item_from_verbose
AFAIK functions do work like that. What you are trying to so is add extra details to your command. You can do it like that:
#!/bin/bash
remove_item_from_verbose="2>&1 | grep -v \"Authorization\""
echo -e "\n +++ Creating '$TENANT' tenant:\n"
# Create tenant
eval curl -L -X PUT "http://localhost:$HOST_PORT/admin/v2/tenants/$TENANT" \
--verbose \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $AUTHORIZATION" \
--data-raw "{\"allowedClusters\": [\"$CLUSTER\"]}" "$remove_item_from_verbose"
Like this, $remove_item_from_verbose becomes a suffix of your command.
Note the addition of eval in front of curl, it is required to have this suffix treated as part of the command.

How to get http request from curl?

I've got a curl request which looks like:
curl -s http://someurl1.com -H 'Accept: application/json' -u 'emv:LgKrAVkFf2c6mr4DFBQUdjBK' -d grant_type='password' -d scope='offline_access' -d username='datafiles#random.com' -d password='456Qwer()' -d acr_values='tenant:sdfdfsdf-e5e7-42d5-9881-sdfsdf' | python -m json.tool
How can I construct a simple HTTP request from it? I've tried to import it with postman but got Error while importing Curl: Zero or Multiple option-less arguments. Only one is supported (the URL)
curl http://someurl1.com -H 'Accept: application/json' -u 'emv:LgKrAVkFf2c6mr4DFBQUdjBK' -d grant_type='password' -d scope='offline_access' -d username='datafiles#random.com' -d password='456Qwer()' -d acr_values='tenant:sdfdfsdf-e5e7-42d5-9881-sdfsdf'
this string can be imported by Postman.

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/

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

how to do a HTTP POST a list of value using cURL

how I do post multiple values to the same key using cURL?
for example when I ran the following to my example.com URL, it complained...is the format correct or is this a problem with the backend not being able to handle the request?
curl -k -H 'Accept: application/json' --user admin:admin example.com -d name=peter -d name=paul -d name=mary
Multiple -d looks fine. The docs said -d name=daniel -d skill=lousy will generate name=daniel&skill=lousy
http://curl.haxx.se/docs/manpage.html#-d
So if you want send an array, you have to use the [] brackets.
-d name[]=peter -d name[]=paul -d name[]=mary
It looks like you can also use
-d "name[]=peter&name[]=paul&name=mary"

Resources