Qore HttpStreamClient Module Reference 1.0
Loading...
Searching...
No Matches

HttpStreamClient Introduction

The HttpStreamClient module provides a convenience API for reading HTTP streaming responses, primarily designed for Server-Sent Events (SSE) responses with Content-Type text/event-stream.

To use this module, use "%requires HttpStreamClient" in your code.

All the public symbols in the module are defined in the HttpStreamClient namespace.

The main classes are:

Usage Example

%modern
%requires HttpStreamClient
%requires json
HTTPClient client({"url": "https://api.example.com"});
# Start streaming request
hash<auto> info;
HTTPResponseStream stream = HttpStreamClient::sendAndStream(client, "POST", "/events",
make_json({"subscribe": "updates"}),
{"Accept": "text/event-stream", "Content-Type": "application/json"},
\info);
# Check response
hash<HTTPResponseStreamInfo> resp = stream.getInfo();
if (resp.status_code != 200) {
throw "HTTP-ERROR", sprintf("HTTP %d: %s", resp.status_code, resp.status_message);
}
# Read SSE events
if (resp.content_type == "text/event-stream") {
while (!stream.isEof()) {
*hash<SseMessageInfo> evt = stream.readServerSentEvent(30s);
if (!evt) {
break;
}
printf("Event: %y, Data: %y\n", evt.event, evt.data);
}
} else {
# Read entire body
*binary body;
while (*binary chunk = stream.readChunk(30s)) {
body += chunk;
}
printf("Response: %s\n", body.toString());
}
stream.close();

Release Notes

HttpStreamClient v1.0

  • initial release