Unable to POST a request to a server using CURL in BASH - string

I have been trying to run a BASH script which posts a request to an SMS server and on successful execution a message is received on the mentioned mobile number. Script is as shown below:
curl -k -X POST "http://192.168.10.3/u=admin&h=452ba065ebd1723598a51c7eca11d362&op=pv&to=1234567891&msg=Hello+to+all"
The above script is working fine. The message "Hello to all" is being received on the mobile number 1234567891. This number is however hard coded in the URL. In the actual scenario the mobile number would be available in a variable and the SMS would be sent to the mobile number available in this variable.
I have tried scripts like:
mobile_number="1234567891"
curl -k -X POST "http://192.168.10.3/u=admin&h=452ba065ebd1723598a51c7eca11d362&op=pv&to=$mobile_number&msg=Message+From+world"
and
x="http://192.168.10.3/u=admin&h=452ba065ebd1723598a51c7eca11d362&op=pv&to="
x+="1234567891
x+=&msg=Hello+to+all"
curl -k -X POST $x
However, i have been unsuccessful in executing them successfully. It would be of great help if someone could help me with the syntax.

Try out this principle, bash is different language than c++ or so :-):
#!/bin/bash
to="1234567891"
msg="Hello+to+all"
u="admin"
hash="452ba065ebd1723598a51c7eca11d362"
op="pv"
ip="192.168.10.3"
url="http://${ip}/u=admin&h=${hash}&op=${op}&to=${to}&msg=${msg}"
echo ${url}
Than : curl -k -X POST $url should work fine.

Related

Redirecting curl REST API Call to File Doesn't Work with Large Payload

This works & writes to the file with no issues:
curl -x myproxy.baseurl.com:80 -H "Authorization: Token mytoken" https://app.api.com/?status=Approved > output.txt
This sends the output to the command line & hangs until I hit ctl-c:
curl -x myproxy.baseurl.com:80 -H "Authorization: Token mytoken" https://app.api.com/?status=Approved&page_size=100000 > output.txt
Are there some kind of payload size limitations? The length of the JSON response in the first call is 270,928 characters. The length of the second, two parameter, call is 622,133.
Could it be a time-out issue? The first payload takes ~5 seconds, and the second payload takes ~11 seconds when testing with SoapUI.
UPDATE: If I don't ctl-c on the second call, eventually curl does time out: curl: (28) connect() timed out!
UPDATE 2: FWIW, this is part of a shell script I am writing, so yeah, it is a programming problem. :P
Make sure to quote your URL because the shell is interpreting the ampersand to put curl in the background.
curl -x myproxy.baseurl.com:80 -H "Authorization: Token mytoken" "https://app.api.com/?status=Approved&page_size=100000" > output.txt

How to get http status code and content separately using curl in linux

I have to fetch some data using curl linux utility. There are two cases, one request is successful and second it is not. I want to save output to a file if request is successful and if request is failed due to some reason then error code should be saved only to a log file. I have search a lot on www but could not found exact solution that's why I have posted a new question on curl.
One option is to get the response code with -w, so you could do it something like
code=$(curl -s -o file -w '%{response_code}' http://example.com/)
if test "$code" != "200"; then
echo $code >> response-log
else
echo "wohoo 'file' is fine"
fi
curl -I -s -L <Your URL here> | grep "HTTP/1.1"
curl + grep is your friend, then you can extract the status code later for your need.

ngrok retrieve assigned subdomain

I've got a NodeJS script which spins up a ngrok instance, which starts the ngrok binary.
However I'm needing to be able to return back the automatically generated url. I can't find anywhere in the documentation about how to do this.
for example, when you run ngrok http 80 it spins up, generates you a random unique url each time it starts
This question is kinda old, however, I thought to give another more generic option as it doesn't require NodeJS
curl --silent --show-error http://127.0.0.1:4040/api/tunnels | sed -nE 's/.*public_url":"https:..([^"]*).*/\1/p'
This one just inspects the response of calling api/tunnels by applying text processing (sed) to the resulted text and identifies the public URL.
ngrok serves tunnel information at http://localhost:4040/api/tunnels.
curl + jq
curl -Ss http://localhost:4040/api/tunnels | jq -r '.tunnels[0].public_url'
=> https://719c933a.ap.ngrok.io
curl + ruby
curl -Ss http://localhost:4040/api/tunnels | \
ruby -e 'require "json"; puts JSON.parse(STDIN.read).dig("tunnels", 0, "public_url")'
=> https://719c933a.ap.ngrok.io
curl + node
json=$(curl -Ss http://127.0.0.1:4040/api/tunnels);
node -pe "var data = $json; data.tunnels[0].public_url"
=> https://719c933a.ap.ngrok.io

curl usage to get header

Why does this not work:
curl -X HEAD http://www.google.com
But these both work just fine:
curl -I http://www.google.com
curl -X GET http://www.google.com
You need to add the -i flag to the first command, to include the HTTP header in the output. This is required to print headers.
curl -X HEAD -i http://www.google.com
More here: https://serverfault.com/questions/140149/difference-between-curl-i-and-curl-x-head
curl --head https://www.example.net
I was pointed to this by curl itself; when I issued the command with -X HEAD, it printed:
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the
Warning: way you want. Consider using -I/--head instead.
google.com is not responding to HTTP HEAD requests, which is why you are seeing a hang for the first command.
It does respond to GET requests, which is why the third command works.
As for the second, curl just prints the headers from a standard request.

curl and cookies - command line tool

I am trying to get more familiar to curl, so I am trying to send post request to a webpage from a site I am already logged into :
curl --data "name=value" http://www.mysite.com >> post_request.txt
I had an output consisting of all the hmtl page with a message telling me I was not logged in so I retrieved the site's cookie (called PHPSESSID, is that of importance) stored it in /temp/cookies.txt and then
curl --cookie /tmp/cookies.txt --cookie-jar /tmp/cookies.txt --data "name=value" http://www.mysite.com > post_request.txt
but I still have the same message. Someone could help ?
I suggest you take a network dumping tool (wireshark or tcpdump) and sniff the communication between your shell and the server. Check if the cookie is sent and if it has the right content.

Resources