Linux print file and use it in for example a curl request - linux

This might be a complete noob question, but I would like to know a simple oneliner to solve my problem.
Imagine following: I have 5 files, could be .txt, .sql .anything.
root#DESKTOP:/$ ls
test.sql test1.sql test2.sql test3.sql
Imagine I would like to use for example test.sql in a curl request as part of a json parameter.
root#DESKTOP:/$ cat test.sql |
curl -H "Accept: application/json"
-H "Content-type: application/json"
-X POST -d "{ \"sqlcommand\" :\"??????" }"
http://localhost:9999/api/testsql
How can I put the output of the cat command in this request at the place of the questionmark? $0, $1 etc are not sufficient. I know how to do it with a for loop. And I could write a shell script that takes an input parameter that I could paste in the command. But. I would like to do it in a simple oneliner and moreover I'd like to learn how I can get the output of the previous command when I need to use it combined with other data OR when it is bad practice i'd like to know the best practice.
Thank you in advance!

This should do what you need:
curl -X POST -d "{ \"sqlcommand\" :\"$(cat test.sql)\" }"
$(cmd) substitutes the result of cmd as a string

Related

How do I send a message with textbelt with user input using bash?

I want to send sms messages with a bash script using textbelt.
When I use read varnumber to save the number, textbelt only sends the first word in the message. Because there is a space in the message, the code ignores the rest of the message.
echo What number would you like to message?
read varnumber
echo What would you like to say?
read varmessage
curl -X POST https://textbelt.com/text \
--data-urlencode phone=$varnumber \
--data-urlencode message=$varmessage \
-d key=textbelt
General rule:
Quote it if it can either be empty or contain spaces (or any whitespace really) or special characters (wildcards). Not quoting strings with spaces often leads to the shell breaking apart a single argument into many.
So putting the variables in your curl query in quotes, should solve the problem.
curl -X POST https://textbelt.com/text \
--data-urlencode phone="$varnumber" \
--data-urlencode message="$varmessage" \
-d key=textbelt
Also, you may want to put your console output into quotes:
echo "Whatever you want to write here"
Or to combine output/input you could use read -p:
read -p "What is your phonenumber? " varnumber

Python 2.7 - optimised way to replace only a part of entire command line

I have a text file say 1.txt from where I am fetching some keywords like "sent", "inbox", "outbox" etc. for it to be replaced in a command line.
Below is how the command line looks:
curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/mainlist/oldtext.txt" -T newtext.txt
I am trying to replace the "mainlist" with the data i fetched from 1.txt file, e.g.
curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/sent/oldtext.txt" -T newtext.txt
curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/inbox/oldtext.txt" -T newtext.txt
I have tried by adding this by creating two strings and concatenating it but looks like I am going wrong.
ltppath=r"/home/Desktop/1.txt"
with open((ltppath),'r') as fh:
ls=fh.readlines()
for line in ls:
string1="curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/"+line.strip()
string2="/oldtext.txt" -T newtext.txt"
conc=string1+string2
cmd=os.system(conc)
Can someone please help me in finding solution for this i am very new to python.
It doesn't make a lot of sense to me to use Python to generate command lines that you then pass back to the shell to execute. You could use a bash script for that, that would be a lot simpler than Python:
while read line; do
curl -H "pqr: thisisalsolink" -X PUT "https://example.com/artifactory/xyz/$line/oldtext.txt" -T newtext.txt
done </home/Desktop/1.txt
If you're doing it with Python, do it with Python. A nice library to do HTTP is called requests (docs).
import requests
upload_headers = {
'pqr': 'thisisalsolink'
}
with open('/home/Desktop/1.txt', 'r') as fh:
for line in fh:
url = 'https://example.com/artifactory/xyz/' + line.strip() + '/oldtext.txt'
with open('/home/Desktop/newtext.txt') as new_txt:
requests.put(url, headers=upload_headers, data=new_txt)

How to add content of file, as part of a terminal command

I have the following command:
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
],
"from": 10,
"size": 10
}
'
I'm trying to divide it into two parts, one part will be saved in a file.
so i can run something like this:
curl -X GET "localhost:9200/bank/_search?pretty" file.txt
How can i achieve this?
To send data that comes from a file, rather than a command line argument, use -d#file.txt:
curl -X GET "localhost:9200/bank/_search?pretty" -d#file.txt
From the curl(1) manual page:
If you start the data with the letter #, the rest should be a
file name to read the data from, or - if you want curl to read
the data from stdin. Posting data from a file named 'foobar'
would thus be done with -d, --data #foobar. When -d, --data is
told to read from a file like that, carriage returns and new‐
lines will be stripped out.
For JSON, stripping line breaks like that fortunately doesn't matter.
You can read the file in place
curl -X GET "localhost:9200/bank/_search?pretty" $(cat file.txt)

Getting the size of a remote file in bytes? (without content-size)

I'd like to get the byte size of a remote file as simply as possible.
The issue is that many servers don't send the content-size parameter in their headers these days
curl -I, wget --spider and wget --server-response all give me detailed headers, but nothing about the size of the content.
curl -Is https://wordpress.com | grep content
only returns this:
content-type: text/html; charset=utf-8
So I guess I could work around it like so:
curl -s https://wordpress.com/ > /tmp/foo; du /tmp/foo | awk '{print $1}'
And it does work. But I think it's a little ridiculous that I'm downloading the file itself and writing to my machine.
I imagine there's a better way, where something like curl can get the file size directly from memory, or just get the length of the output to bash in bytes.
The content-size header is not guaranteed in every response. So best you can do might be, to use content-size if available and if not, call curl to download and assess the size of the remote content:
curl -sL 'https://wordpress.com' --write-out '%{size_download}\n' --output /dev/null

Azure hdinsight action script not running (works locally) [duplicate]

My script is taking the command line argument and it needs to pass this to curl command in double quotes. This is the simplified version of what I tried so far:
json=$1;
echo $json;
curl -X POST -d '{"asin":\"$json\", "template":"bolt","version":"1d"}' -H "Content-Type: application/json" http://someURL
But its not working. Please help
$-variables in single quoted strings don't get expanded. The -d argument needs be in double quotes, or at least the $json part needs to be:
curl -X POST -d '{"asin":"'"$json"'", "template":"bolt","version":"1d"}' -H "Content-Type: application/json" http://someURL
'-terminates the single-quoted string, then "$json" follows, and then ' starts an adjacent single quoted string.
The "$json" variable shouldn't expand to a string containing unescaped double quotes or the resulting json will be broken.

Resources