Crafter.CppDOM/examples/FetchExample
2025-11-14 18:40:13 +01:00
..
main.cpp lifecycle update 2025-11-14 18:40:13 +01:00
project.json fetch 2025-11-11 00:21:11 +01:00
README.md fetch readme 2025-11-11 00:25:24 +01:00
run.sh fetch 2025-11-11 00:21:11 +01:00

FetchExample

This sample demonstrates how to use the Fetch API in Crafter.CppDOM to make HTTP requests.

Features

  • Shows how to perform HTTP GET requests
  • Demonstrates asynchronous response handling
  • Illustrates how to update the DOM with fetched data

Usage

import Crafter.CppDOM;
import std;
using namespace Crafter::CppDOMBindings;

int main(){
    void* body = GetElementById("body");
    SetInnerHTML(body, "<h1>Fetch Example</h1><p>Testing HTTP requests...</p>");
    
    Fetch("https://httpbin.org/get", [body](std::string result){
        if (!result.empty()) {
            SetInnerHTML(body, "<h1>Fetch Example</h1><p>Response: " + result + "</p>");
        } else {
            SetInnerHTML(body, "<h1>Fetch Example</h1><p>Failed to fetch data</p>");
        }
        FreeJs(body);
    });
}

Building and Running

crafter-build build executable
run.sh

Then navigate to http://localhost:8080/ in your browser.

If caddy is not installed, you can use your favorite static file server instead.