HTTP also uses a head and body. An API normally show you information too, but that doesn’t happen often in de browser or by using curl.
curl localhost:1901 --verbose
HEAD
DNS and TCP/IP start with *
Request to server start with >
Response sent from server start with <
Request head starts with a “request line”: bijv. GET / HTTP/1.1
After path has been requested from the server / . HTTP (protocol) version (1.1).
Options request to / like so: curl localhost:1901 —verbose —request OPTIONS. to get other HTTP methods
--request
curl localhost:1901 --verbose --request OPTIONS
User-agent = tells the server who’s contacting it
Accept = tell the server what kind of data the client would like
/ = anything.
200 = OK
Date = current time on the server
Allow (when options are requested) = list of allowed methods for the resource.
Not listed method Delete. — request DELETE to /.
curl localhost:1901 --verbose --request DELETE
“ error - delete is not allowed for / “
HTTP/1.1 405 Method not Allowed
Status code 4xx - client made an error.
Method DELETE is not supported.
curl localhost:1901 --verbose --request HEAD
HEAD is the same as GET, but without the body.
POST is used to add something to a resource.
if ‘/‘ (the server) is a list of movies POST adds a movie to a list.
POST
curl localhost:1901 —verbose --request POST
400 Bad request.
Expect an added movie in json format.
Upload data —request POST with an empty json object { }.
curl localhost:1901 --verbose --request POST --data '{}'
422 Unprocessable Entity = request was valid, but something wrong with the data being sent to the server.
curl localhost:1901 --verbose --request POST --data '{"title":"Wonder Woman"}'
201 created
location: /wonder-woman id an title are added.
plot and description will be added later.
curl localhost:1901/wonder-woman --verbose --request OPTIONS
200 OK
Head is useful for checking if something exists.
curl localhost:1901/nonexistent-movie --verbose --request HEAD
404 Not found
PUT
PUT is used to place a resource somewhere. POST adds something to /. PUT places that something at exactly that place
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
curl localhost:1901/wonder-woman --verbose --request PUT --data ‘{“title":"Wonder Woman","plot":"Diana fights a war”}’
PUT is nice for saving the resource, but you need to send other properties like ‘title’ to.
PATCH is used to only change a few things.
curl localhost:1901/wonder-woman --verbose --request PATCH --data '{"description":"Diana leaves home to fight a war."}'
DELETE
curl localhost:1901/wonder-woman --verbose --request DELETE
204 No content
2xx range = successful request.
curl localhost:1901/wonder-woman --verbose --request GET
GET request to see if Wonder Woman is removed.
410 Gone = there used to be something there, but it no longer exists.
API returns json data.
API can also return XML data, HTML and CSV and other files.
Get file
Get XML
curl localhost:1901 --verbose --request GET --header 'Accept: application/xml'
Content-type: application/xml
json is more comprehendible than xml, especially when you know Javascript.
json files can be big, and if the server supports encoding the server will send back compressed data. Like gzip.
curl localhost:1901 --verbose --request GET --header 'Accept-Encoding: gzip'
200 OK
Content-encoding gzip
Authorization
After the server has been made secure and not everyone has authorization to it.
curl localhost:1901/evil-dead --verbose --request DELETE
401 unauthorized = may be possible but you have no rights to do so.
How to authorize a request?
curl localhost:1901/evil-dead --verbose --request DELETE --header 'Authorization: token 5b8822b29'
204 No content
Important
Now, keep in mind, even though HTTP is a standard protocol, not all servers
use the same methods or status codes. For example, OPTIONS and PATCH are
often not used, and 400 Bad Request is often used instead of other 4xx
errors.
Cheatsheet HTTP
HTTP also uses a head and body. An API normally show you information too, but that doesn’t happen often in de browser or by using curl.
curl localhost:1901 --verbose
HEAD
DNS and TCP/IP start with * Request to server start with > Response sent from server start with < Request head starts with a “request line”: bijv. GET / HTTP/1.1 After path has been requested from the server / . HTTP (protocol) version (1.1).
Options request to / like so: curl localhost:1901 —verbose —request OPTIONS. to get other HTTP methods
--request
curl localhost:1901 --verbose --request OPTIONS User-agent = tells the server who’s contacting it Accept = tell the server what kind of data the client would like / = anything.
200 = OK Date = current time on the server Allow (when options are requested) = list of allowed methods for the resource. Not listed method Delete. — request DELETE to /.
curl localhost:1901 --verbose --request DELETE “ error - delete is not allowed for / “ HTTP/1.1 405 Method not Allowed Status code 4xx - client made an error. Method DELETE is not supported.
curl localhost:1901 --verbose --request HEAD HEAD is the same as GET, but without the body. POST is used to add something to a resource. if ‘/‘ (the server) is a list of movies POST adds a movie to a list.
POST
curl localhost:1901 —verbose --request POST 400 Bad request. Expect an added movie in json format. Upload data —request POST with an empty json object { }.
curl localhost:1901 --verbose --request POST --data '{}' 422 Unprocessable Entity = request was valid, but something wrong with the data being sent to the server.
curl localhost:1901 --verbose --request POST --data '{"title":"Wonder Woman"}' 201 created location: /wonder-woman id an title are added. plot and description will be added later.
curl localhost:1901/wonder-woman --verbose --request OPTIONS 200 OK Head is useful for checking if something exists.
curl localhost:1901/nonexistent-movie --verbose --request HEAD 404 Not found
PUT
PUT is used to place a resource somewhere. POST adds something to /. PUT places that something at exactly that place
curl localhost:1901/wonder-woman --verbose --request PUT --data ‘{“title":"Wonder Woman","plot":"Diana fights a war”}’ PUT is nice for saving the resource, but you need to send other properties like ‘title’ to.
PATCH is used to only change a few things. curl localhost:1901/wonder-woman --verbose --request PATCH --data '{"description":"Diana leaves home to fight a war."}'
DELETE
curl localhost:1901/wonder-woman --verbose --request DELETE 204 No content 2xx range = successful request.
curl localhost:1901/wonder-woman --verbose --request GET GET request to see if Wonder Woman is removed. 410 Gone = there used to be something there, but it no longer exists. API returns json data. API can also return XML data, HTML and CSV and other files.
Get file
Get XML curl localhost:1901 --verbose --request GET --header 'Accept: application/xml' Content-type: application/xml
json is more comprehendible than xml, especially when you know Javascript. json files can be big, and if the server supports encoding the server will send back compressed data. Like gzip. curl localhost:1901 --verbose --request GET --header 'Accept-Encoding: gzip' 200 OK Content-encoding gzip
Authorization
After the server has been made secure and not everyone has authorization to it. curl localhost:1901/evil-dead --verbose --request DELETE 401 unauthorized = may be possible but you have no rights to do so.
How to authorize a request? curl localhost:1901/evil-dead --verbose --request DELETE --header 'Authorization: token 5b8822b29' 204 No content
Important
Now, keep in mind, even though HTTP is a standard protocol, not all servers use the same methods or status codes. For example, OPTIONS and PATCH are often not used, and 400 Bad Request is often used instead of other 4xx errors.