diff --git a/docs/source/examples/greet-json.rst b/docs/source/examples/greet-json.rst deleted file mode 100644 index 76d851e..0000000 --- a/docs/source/examples/greet-json.rst +++ /dev/null @@ -1,17 +0,0 @@ -Working with JSON -================== - -Nowadays webservices are json based so making your script json aware is probably -a good chioce. In order to be able to extract data from and compose json -documents from a script you can use -`jq `_. - -In this example we extract the name field from the incomming json document in -order to generate a two attribute json response. - -.. code-block:: bash - - who=$(kapow get /request/body | jq -r .name) - kapow set /response/headers/Content-Type "application/json" - kapow set /response/status 200 - jq --arg greet "Hello" --arg value "${who:-World}" -n \{greet:\$greet\,to:\$value\} | kapow set /response/body diff --git a/docs/source/examples/index.rst b/docs/source/examples/index.rst index 81cbcdb..3d0fa86 100644 --- a/docs/source/examples/index.rst +++ b/docs/source/examples/index.rst @@ -225,6 +225,13 @@ Testing with curl: Modify JSON by using shell ++++++++++++++++++++++++++ +.. note:: + + Nowadays webservices are json based so making your script json aware is probably a good choice. In order to be able to extract data from and compose json documents from a script you can use + `jq `_. + +**Example 1** + In this example our Kapow! service will receive a JSON value with an incorrect date, then our .pow file will fix then and return the correct value to the user. .. code-block:: console @@ -246,9 +253,40 @@ Call service with curl: "incorrectDate": "2019-11-22_10-42-06" } +**Example 2** + +In this example we extract the name field from the incoming json document in order to generate a two attribute json response. + +.. code-block:: console + + $ cat echo-attribute.pow + kapow route add -X POST '/echo-attribute' - <<-'EOF' + JSON_WHO=$(kapow get /request/body | jq -r .name) + + kapow set /response/headers/Content-Type "application/json" + kapow set /response/status 200 + + jq --arg greet "Hello" --arg value "${JSON_WHO:-World}" -n \{greet:\$greet\,to:\$value\} | kapow set /response/body + + EOF + +Call service with curl: + +.. code-block:: console + :linenos: + :emphasize-lines: 4 + + $ curl -X POST http://localhost:8080/echo-attribute -H "Content-Type: application/json" -d '{"name": "MyName"}' + { + "greet": "Hello", + "to": "MyName" + } + Upload files ++++++++++++ +**Example 1** + Upload a file using Kapow! is very simple: .. code-block:: console @@ -267,6 +305,36 @@ Upload a file using Kapow! is very simple: $ curl -X POST -H "Content-Type: multipart/form-data" -F "data=@results.json" http://localhost:8080/upload-file {"hello": "world"} +**Example 2** + +In this example we respond back with the line count of the file received in the request: + +.. code-block:: console + :linenos: + + $ cat count-file-lines.pow + kapow route add -X POST '/count-file-lines' - <<-'EOF' + + # Get sent file + FNAME="$(kapow get /request/files/myfile/filename)" + + # Counting file lines + LCOUNT="$(kapow get /request/files/myfile/content | wc -l)" + + kapow set /response/status 200 + + echo "$FNAME has $LCOUNT lines" | kapow set /response/body + EOF + +.. code-block:: console + :linenos: + + $ cat file.txt + hello + World + $ curl -F "myfile=@file.txt" http://localhost:8080/count-file-lines + file.txt has 2 lines + Protecting again Command Injection Attacks ++++++++++++++++++++++++++++++++++++++++++ @@ -444,8 +512,3 @@ Calling with curl: < Ok * Connection #0 to host localhost left intact - -TODO: -+ QUITAR LOS COMMAND IJECTIONS -+ corregir los :samp: -- negritas diff --git a/docs/source/examples/upload.rst b/docs/source/examples/upload.rst deleted file mode 100644 index 26e73ec..0000000 --- a/docs/source/examples/upload.rst +++ /dev/null @@ -1,20 +0,0 @@ -Upload a file -============= - -HTTP request allows us to send and receive files by using the Multipart standard. - -Kapow! allow us to handle files received in the request. In this example we -respond back with the line count of the file received in the request. - -.. code-block:: bash - - fname=$(kapow get /request/files/myfile/filename) - lcount=$(kapow get /request/files/myfile/content | wc -l) - kapow set /response/status 200 - echo "$fname has $lcount lines" | kapow set /response/body - -You can try this by using the following curl: - -.. code-block:: bash - - curl -F "myfile=@README.rst" http://localhost:8080/linecount