Skip to content

Instantly share code, notes, and snippets.

@grant
Created February 13, 2021 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grant/b1d46232508d33df2ad9bac7061df4d9 to your computer and use it in GitHub Desktop.
Save grant/b1d46232508d33df2ad9bac7061df4d9 to your computer and use it in GitHub Desktop.
Hello World, C++ Functions Framework
#include <google/cloud/functions/http_request.h>
#include <google/cloud/functions/http_response.h>
#include <nlohmann/json.hpp>
namespace gcf = ::google::cloud::functions;
gcf::HttpResponse hello_world_http(gcf::HttpRequest request) {
auto greeting = [r = std::move(request)] {
auto request_json = nlohmann::json::parse(r.payload(), /*cb=*/nullptr,
/*allow_exceptions=*/false);
if (request_json.count("name") && request_json["name"].is_string()) {
return "Hello " + request_json.value("name", "World") + "!";
}
return std::string("Hello World!");
};
gcf::HttpResponse response;
response.set_payload(greeting());
response.set_header("content-type", "text/plain");
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment