12.9.1 How it works
requestNetwork()gives aNetworkCap.HttpRequest(method, url)creates a request with anHttpMethod:GET,HEAD,POST,PUT,PATCHorDELETE. Requests are immutable:withHeader,withTextBodyorwithTimeoutreturn a new request.network.send(request)returnsResult<HttpResponse, IoError>.
IoError.The program sends a GET request to the server and receives a 200 response with its body. Responses, including 404 and 500, are Success; transport failures are Error.
ProgramAsking for the weather
Subroutine ConsultarClima() As Result<String, IoError>
Define red As NetworkCap <- Propagate requestNetwork();
Define peticion As HttpRequest <- HttpRequest(HttpMethod.GET, "https://clima.test/hoy");
Define respuesta As HttpResponse <- Propagate red.send(peticion);
Write "Estado: ", respuesta.status();
Return respuesta.textBody();
EndSubroutine
Algorithm Clima
Switch ConsultarClima() Do
Success(texto): Write texto;
Failure(problema): Write problema.code;
EndSwitch
EndAlgorithmOutput
Estado: 200 Soleado, 25
The program finishes normally.
A response with a status from 100 to 599 is always Success; isSuccess() says whether it is between 200 and 299. Methods that can modify the server —POST, PUT, PATCH, DELETE— can end up in an uncertain state if the connection drops half-way: the program cannot know whether the change happened.
In short
requestNetwork,HttpRequestandsend: capability, request and response.- HTTP statuses are responses; network failures are
IoError. - Requests are immutable and the limits belong to the host.