httpResponseBody (Function)
Signature: httpResponseBody(responseID: int) -> Result<string, Error>
Description: Returns a copy of the response body as Result<string, Error>. An invalid or released handle returns Error; the copied body survives httpResponseFree.
Parameters
- responseID (
int): Handle returned by httpGetResponse
Returns: Result<string, Error>
Example
fn inspect(response) = {
let summary = "status=${httpResponseStatus(response)}; body=${httpResponseBody(response)}; header=${httpResponseHeader(response, "X-Demo")}"
let released = httpResponseFree(response)
"${summary}; released=${released}"
}
fn reply(_method, _path, _headers, _body) = HttpResponse {
status: 200, headers: "X-Demo: hi\r\n", contentType: "text/plain",
streamFd: -1, isComplete: true, partialBody: "hello"
}
fn runRequest(client) = {
let result = match httpGetResponse(client, "/", "") {
Success { value } => inspect(value)
Error { message } => message
}
let closed = httpCloseClient(client) ?: -1
"${result}; client close=${closed}"
}
fn request() = match httpCreateClient("http://127.0.0.1:18898", 1000) {
Success { value } => runRequest(value)
Error { message } => "client error: ${message}"
}
fn serve(server) = {
let result = match httpListen(server, reply) {
Success => request()
Error { message } => "listen error: ${message}"
}
let stopped = httpStopServer(server) ?: -1
"${result}; server stop=${stopped}"
}
print(match httpCreateServer(18898, "127.0.0.1") {
Success { value } => serve(value)
Error { message } => "server error: ${message}"
})
inspect response =
summary = "status=${httpResponseStatus response}; body=${httpResponseBody response}; header=${httpResponseHeader (response, "X-Demo")}"
released = httpResponseFree response
"${summary}; released=${released}"
reply (_method, _path, _headers, _body) = HttpResponse(
status = 200, headers = "X-Demo: hi\r\n", contentType = "text/plain",
streamFd = -1, isComplete = true, partialBody = "hello")
runRequest client =
result = match httpGetResponse (client, "/", "")
Success value => inspect value
Error message => message
closed = httpCloseClient client
"${result}; client close=${closed}"
request () =
client = httpCreateClient ("http://127.0.0.1:18898", 1000)
match client > 0
true => runRequest client
false => "client error: ${client}"
serve server =
listening = httpListen (server, reply)
result = match listening == 0
true => request ()
false => "listen error: ${listening}"
stopped = httpStopServer server
"${result}; server stop=${stopped}"
server = httpCreateServer (18898, "127.0.0.1")
match server > 0
true => print (serve server)
false => print "server error: ${server}"