Week Lessons 2

Use curl to post data

While developing an api, I needed to do some posts to a certain url. The fastest way I could think of was to use the command line curl command. However, by default curl does a GET request, and of course, sends no data. However a short googling trip took me to this superuser answer, which demonstrates how to do it. I was posting json to my api endpoint in no time. Example:

curl -X POST -d @filename.json http://example.com/api/endpoint/ --header "Content-Type:application/json"

Which will do a post using data from filename.json to the given url. The request header content type is also set to json. Of course if your not posting json this should be changed to reflect the data.

Some bash script tips

Every morning do a refresh of all my repositories so that I know I don't fall behind on the code if I need to solve some problem or continue developing. Doing this manually is not practical, and so I created a bash script to refresh all my repositories. While I was at it I added a starting command for the apache and mysql. Since I do not desire to start them if they are already running, the script check first if they are already running. Writing this script I learned a lot about shell scripting. Here are some of the things I picked up

Adding -x to the shebang will make bash output everything that is going on, helps with debugging

Giving a variable as a parameter to a function, if you just give the variable name it will be treated as a string value and that will be passed to the function. I ended up encapsulating it as "${var}" since the value was a string. If your value is not a string you probably need some other solution.

Comparing a variable to a string is best achieved with the following syntax: if[ "${var}" = "foo" ]; then Notice the space between the brackets and the values inside.

Using grep to pick something out of a list of processes requires an extra step in which you remove the grep itself from the list. ps -e | grep -v grep | grep searchstring Otherwise grep might find its own search process, which is of course not the desired effect.

Hopefully this will save you some time in scripting.