Using CURL in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using CURL in a script
# 1  
Old 04-09-2012
Using CURL in a script

Hi all

so I'm new to scripting but I am making a script that is a url shortener. It will take a url off the command line and spit back the shortened version using bit.ly's api

this is what I have so far

Code:
if [ $# -lt 1 ]
then
        echo "You must supply a URL."
        exit 1
fi

read url
curl "https://api-ssl.bitly.com/v3/shorten?access_token=xxxxxxxxxxxxxxxxxxxxxx&longUrl=$url"

curl -d

I am not entirely sure if I am using this correctly. I know curl will fetch the direct URL for the conversion but I dont know if it is legal to stuff '$url' directly into the http address. Curl -d will post the data but I'm a bit confused as to what URL I'm supposed to use there. . .

basically what i want to do is fetch the url off the command line and stuff it into that http address after '&longURL=' where the url is supposed to be. Then I need to fetch back the shortened version of the address.

this is the api
ApiDocumentation - bitly-api - bitly REST API method documentation - API Libraries and Documentation for bitly - Google Project Hosting
and this is the page ive been reading regarding curl
cURL - Tutorial

I believe curl -d will post data granted I provide the information after it to post it but I dont know what should go after curl -d. .I am new to using APIs and im just picking this up

any help is appreciated thank you
# 2  
Old 04-09-2012
I just tried and it takes GET arguments, -d will POST data.

Code:
curl "https://api-ssl.bitly.com/v3/shorten/?login=<user>&apiKey=<key>&format=txt&longUrl=$url"

works for me.

Also you said it takes URL from command line, you'd simply use $1 instead of "read url" which would prompt again

If you need more help with the script, please let us know the target shell -- posix, ksh, bash, etc.
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 04-09-2012
thanks for the reply I am using bash shell

as for posting data, how can I use 'curl -d' to post the short URL? Is it going to automatically return the short URL on its own or is there a specific address I need to use from the API?

I am looking at this here

ApiDocumentation - bitly-api - bitly REST API method documentation - API Libraries and Documentation for bitly - Google Project Hosting

thanks again
# 4  
Old 04-09-2012
For shell scripting I'd recommend using format=txt and then the result is simply the shortened url. This may get you started

Code:
#!/bin/bash

if (( $# < 1 )); then
    echo "You must supply a URL."
    exit 1
fi

if short=$(curl "https://api-ssl.bitly.com/v3/shorten?access_token=xxxxxxxxxxxxxxxxxxxxxx&format=txt&longUrl=$1" 2>/dev/null); then
    echo "Short URL: $short"
else
    echo "Communication error"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl not accepting spaces in script via variables

Hi All, I'm trying to run a script which issues rest commands via curl to an endpoint. If I put spaces in fields via something like insomnia, it works, but when I try from an input file, it's failing with a json error. while IFS=, read mname oname <------ my input file... (10 Replies)
Discussion started by: say170
10 Replies

2. Shell Programming and Scripting

Using curl in bash script

Hello. I have pick up a script from internet to track errors from curl command. #!/bin/bash # URL_TO_TEST="http://www.xxxxxx.yyy" MY_VAR=curl_init("$URL_TO_TEST") ; curl_setopt($MY_VAR, CURLOPT_HEADER, 1); curl_setopt($MY_VAR, CURLOPT_RETURNTRANSFER, 1); curl_setopt($MY_VAR,... (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Script containing Curl doesn't run with crontab

Hello I have a problem with the crontab command when I run a code containing Curl on the command line it runs without fail but as soon as I program it with crontab it executes everything except the curl returns fail thank you for helping me to resolve this problem because since Monday I look... (14 Replies)
Discussion started by: beautymind
14 Replies

4. Shell Programming and Scripting

Rest APIs without curl in shell script

We are not allowed to install curl on our linux box. Is there any other way to talk to Rest API's in shell script rather than using curl ? - Please advise - thank you (3 Replies)
Discussion started by: rv_champ
3 Replies

5. Shell Programming and Scripting

curl into a shell script (variable transmission): need help

Hey I'm writting a little shell script, and I started using curl with it today. I would like to login into a website using curl (--data argument). This part is working, curl sent me the webpage that we see when we log in this website. Problem: This is a temporary page. When I log in this... (0 Replies)
Discussion started by: Link_
0 Replies

6. Shell Programming and Scripting

curl ftp script

Hello - I'm a novice, so apologies if this is a stupid/answered question. I'm operating on a mac, and I'm using ftp to transfer files to my computer. The files have atypical names, although I have a list of them, addresses included. Is there a way to automate this using curl? That is to... (3 Replies)
Discussion started by: Qroid
3 Replies

7. Shell Programming and Scripting

using curl in a script

Hello everyone I'm currently using a script to upload files (different file names, but same date) to an ftp server (see below) #!/bin/sh # Set the variables HOST=<host> USER=<user> PASSWD=<password> ftp -i -n $HOST <<END_SCRIPT user ${USER} ${PASSWD} mput... (0 Replies)
Discussion started by: soliberus
0 Replies

8. Shell Programming and Scripting

Bash script idea using cUrl -- possible?

I have an alias already in my .bash_profile to download files using cUrl's -o (output to file, user provides the file name) option. I find I'm using it quite a bit, so I wanted to write a script to run "curl -o", taking the necessary inputs - file name and URL from which to download - and then... (3 Replies)
Discussion started by: SilversleevesX
3 Replies

9. Shell Programming and Scripting

using curl with shell script.

hi please help me out here, i want to use curl command in shell script to test web pages, what i have is an opening page, when i click on a button on opening page, the next page comes up and then i have to upload a file n then click another button to submit and then comes the output... (0 Replies)
Discussion started by: ankushg002
0 Replies

10. Shell Programming and Scripting

script to output curl result as html

hi, new to scripting and would like to know how can I have a script which will curl a few URLs and have the results such as the URLs being curled, dns lookup time, connection time, total time, etc save in a html format in a form of table with column and rows. thank you. (4 Replies)
Discussion started by: squidusr
4 Replies
Login or Register to Ask a Question