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.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. 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, fetch in JavaScript, requests in 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, B
    • A, B, C
    • B, A, C
    • C, A, B
    Show the answer

    A, C, B

    fetch returns at once with a promise: the program carries on and prints C. The function passed to .then only 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.

    await only exists in an async function (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 Unauthorized since 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 GET to POST
    Show the answer

    The token has expired: get another one

    401 says “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 a 5xx, a wrong URL a 404.