4.4_transfer_assignment - MartijnKeesmaat/dating-app GitHub Wiki

My notes of the Transer assignment.

The movie API exists of movie each with an:

  • id - unique identifier
  • title - title
  • plot - short description
  • description - long description

Methods

  • Get, get data
  • Delete, remove data
  • Post, add data
  • Head, get header meta data

Curl

Curl is a command line application that can send HTTP requests. Just like your browser, but with more options.

curl is used in command lines or scripts to transfer data. It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, settop boxes, media players and is the internet transfer backbone for thousands of software applications affecting billions of humans daily.

https://curl.haxx.se/

Verbose

Inspect the headers sent to and from the API using Curl's verbose mode, by running the following code in the other tab of your terminal:

curl localhost:1901 --verbose

Which returned

Rebuilt URL to: localhost:1901/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 1901 (#0)
> GET / HTTP/1.1
> Host: localhost:1901
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 07 Mar 2019 14:35:29 GMT
< Connection: keep-alive
< Content-Length: 2266

The headers with the status, type, date

And then the response, in this case the JSON data

Lines with > are the request

Lines with < are the response

Options

To see which options are available for the curl command you can use —request OPTIONS at the end.

User-Agent — tells the server some info on who is contacting it

Accept — tells the server what kind of data the client would like, / means anything)

DELETE

To delete the data you can use the "DELETE" method if you have the right permissions.

curl -X "DELETE" localhost:1901

The -X is in caps, doesn't work with lowercase

HTTP/1.1 405 Method Not Allowed

Status codes in the range of 4xx mean that the client made an error. In this case, 405 Method Not Allowed, means that the method (DELETE) is not supported.

HEAD

The Allow header also mentioned HEAD (and POST). HEAD is like GET but tells the server not to send any data back. Basically the same as GET but without the body.

POST

Post is used to send data.

PUT

PUT is used to place a resource somewhere, whether it exists already or not. Remember that POST is used to add something to a resource? Posting to / adds a movie? PUT places that something at exactly that place.

To summarise:

  • POST to / to add a movie to a list of movies
  • PUT to /wonder-woman to place a movie there, if it exists or not

At this point I got stuck (step 12/19)

"errors": [
    {
      "id": 405,
      "title": "Not allowed",
      "detail": "POST is not allowed for /wonder-woman"
    }
  ],

Therefore I missed patch, delete & no content, gone, accept, accept-encoding, unauthorized, authorized.

Patch

The HTTP PATCH request method applies partial modifications to a resource.