http and api Flashcards
what does HTTP stand for
hypertext transfer protocol
what does API stand for
application programming interface
what are the RESTful HTTP methods?
GET, POST, PATCH, PUT, DELETE
which HTTP methods are safe? and what does safe mean?
GET – safe means that you are only viewing the resource, not changing it
which methods are idempotent? what does this mean?
everything except POST – idempotent means that no matter how many times you call the method, it returns the same result. for everything except POST, you are operating on the same resource every time. POST creates something new every time you call it.
what is statelessness?
REST communication is always stateless. this means that each request from client to server always carries all info needed. the server does not need to remember previous info.
what is REST?
representational state transfer. set of architectural design principles for HTTP APIs.
anatomy of a request
HTTP method, resource/url, headers, body
anatomy of a response
status code, headers, body
what code is an informational response?
1xx
which codes indicate success? creation?
2xx. 200 = request succeeded, 201 = resource created successfully.
which codes indicate that the client needs to take additional action?
3xx. 301 moved permanently. 302 found, resource at diff url temporarily. 307, temp redirect.
which codes indicate problems with a client’s request?
4xx. 400 invalid syntax or params, 404 not found, 422 unprocessable content, 403 forbidden.
which code indicates server error?
500
dynamic vs static path parts
static -> /users/. dynamic -> {user_id}.
good design conventions for paths and queries
paths:
* use nouns
* plural nouns for collections
* be consistent with plurals
* don’t nest too deeply (too long of a path) but use nesting for hierarchies
* use kebab-case or lowercase
* ids belong in paths not queries
queries:
* use for filtering. ? indicates start of query.
body
used for POST and PUT methods that need more complex data. not visible in url.
headers
carry additional metadata. authorization, content-type, accept. accept = what date type client expects. content-type = data type being sent or received.
general format of API method decorator and function
@app.[METHOD] ("/route") def function_name() -> returnType
you might also have {id} if it’s GET, PATCH, PUT, or DELETE in the route