...

/

Solution 1: Working with REST APIs

Solution 1: Working with REST APIs

Let’s solve the challenge set in the previous lesson.

We'll cover the following...

Solution

Here is the updated code for the rServer.go RESTful server to create a single RESTful server that supports image file upload and retrieval.

Here are the commands to run the client:

# To check the connection between server and client:
curl localhost:1234/
# To add a new user to the server:
curl -H 'Content-Type: application/json' -d '{"user": "mtsouk",
"password" : "admin"}' http://localhost:1234/add -v
# To add the same user:
curl -H 'Content-Type: application/json' -d '{"user": "mtsouk",
"password" : "admin"}' http://localhost:1234/add
# To add another user:
curl -H 'Content-Type: application/json' -d '{"user": "mihalis",
"password" : "admin"}' http://localhost:1234/add
# To get information about an existing user:
curl -X GET -H 'Content-Type: application/json' -d '{"user":
"mtsouk", "password" : "admin"}' http://localhost:1234/get
# To delete the user:
curl -H 'Content-Type: application/json' -d '{"user": "mtsouk",
"password" : "admin"}' http://localhost:1234/delete -X DELETE
# Change directory to usercode:
cd usercode
# Upload and save a file named packt.png to the server:
curl -X PUT localhost:1234/files/packt.png --data-binary @packt.png
# Save the file packt.png as 1.png on the server:
curl -X PUT localhost:1234/files/1.png --data-binary @packt.png
# Download 1.png in the usercode as downloaded.png:
curl -X GET localhost:1234/files/1.png --output downloaded.png
...