Consuming a web service
Calling a web service from a program: asynchronous call, promise, reading the response, HTTP errors, timeouts, authentication token, pagination.
- 25 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
HTTP client: what is it?
Show the answer
The part of a program that builds a request (URL, method, headers, body), sends it to a server and reads the response.
Browser,
curl,fetchin JavaScript,requestsin Python: all are HTTP clients. The server only sees the request, not the language that wrote it.In which order does this program print the letters, once the server has answered?
```javascript
console.log("A");
fetch(url).then(() => console.log("B"));
console.log("C");`A,C,BA,B,CB,A,CC,A,B
Show the answer
A,C,Bfetchreturns at once with a promise: the program carries on and printsC. The function passed to.thenonly runs after the current code ends, when the response is there.In a function declared …, the … keyword waits for a promise to settle before moving to the next line, without blocking the rest of the program.
Show the answer
In a function declared async, the await keyword waits for a promise to settle before moving to the next line, without blocking the rest of the program.
awaitonly exists in anasyncfunction (or at the top level of a module). While waiting, the program runs the code that follows the function call.Asynchronous call: what is it?
Show the answer
A call that returns immediately and whose result arrives later, while the program carries on.
A network response takes tens of milliseconds or more; waiting in place would freeze the interface. The code that depends on the response runs when it arrives.
A call has been answering
401 Unauthorizedsince this morning although the code has not changed. What is the most likely cause and the fix?- The server is down: retry in a loop until it answers
- The URL is wrong: add a trailing slash
- The token has expired: get another one
- The method is wrong: switch from
GETtoPOST
Show the answer
The token has expired: get another one
401says “I do not know who you are”: credentials missing, wrong or out of date. An expired token is the commonest case when nothing changed in the code. An outage would give a5xx, a wrong URL a404.