Beispiel HTTP GET

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of HTTP_GET by th.iotkit2.ch

cgi-bin/rest.txt

Committer:
stefan1691
Date:
2016-03-18
Revision:
9:d95493db94c6
Parent:
8:7d8db739ea8c

File content as of revision 9:d95493db94c6:

#!/bin/bash
#
#   Abhandlung der REST Methoden GET, POST, PUT, DELETE

# HTTP Header fuer Client
echo "Content-type: text/plain"
echo ""

# / entfernen damit nicht in andere Verzeichnisse geschrieben werden kann
QUERY_STRING=`echo $QUERY_STRING | sed 's:/::g'`

#
### GET
#
if [ "$REQUEST_METHOD" = "GET" ]
then
    if [ "$PATH_INFO" = "/time" ]   
    then
        date "+%Y.%m.%d %H:%M:%S"
    elif [ "$PATH_INFO" = "/timestamp" ] 
    then
        date "+%s"
    else
        printf "hello $PATH_INFO from `hostname` at `date '+%Y.%m.%d %H:%M:%S'`\n" 
    fi
fi

#
### POST
#
if [ "$REQUEST_METHOD" = "POST" ]
then
    # Zerlegt den Querystring in Einzelteile
    saveIFS=$IFS
    IFS='=&'
    parm=(`cat -`)
    IFS=$saveIFS

    echo "append to http://`hostname`/data/post.txt"
    printf "${REMOTE_ADDR} `date "+%Y.%m.%d %H:%M:%S"` " >>/var/www/html/data/post.txt
    for (( index = 0; index < ${#parm[@]}; index += 2 ))
    do
        printf "${parm[index]} = ${parm[index+1]} " >>/var/www/html/data/post.txt
    done
    printf "\n" >>/var/www/html/data/post.txt
fi

#
### PUT
#
if [ "$REQUEST_METHOD" = "PUT" ]
then
    echo "write to http://`hostname`/data/$QUERY_STRING"
    cat - >/var/www/html/data/$QUERY_STRING
fi

#
### DELETE
#
if [ "$REQUEST_METHOD" = "DELETE" ]
then
    echo "delete http://`hostname`/data/$QUERY_STRING"
    rm -f /var/www/html/data/$QUERY_STRING
fi