Skip to content

Instantly share code, notes, and snippets.

@fernandoc1
Created September 12, 2019 19:45
Show Gist options
  • Save fernandoc1/d4f60f8926bc9b334ce8925ac5f7ed06 to your computer and use it in GitHub Desktop.
Save fernandoc1/d4f60f8926bc9b334ce8925ac5f7ed06 to your computer and use it in GitHub Desktop.
This code tests CGI with C++ and javascript fetch function.
<html>
<body>
</body>
</html>
<script>
async function loadData()
{
let response = await fetch("http://localhost/path/to/TestCGI.bin", {
method: 'POST',
body: "Test fernando"
});
let data = await response.text();
document.body.innerHTML = data;
}
loadData();
</script>
#include <cstdio>
#include <iostream>
#include <QString>
int main(int argc, char** argv)
{
printf("Access-Control-Allow-Origin: *\n");
printf("Content-Type: application/json\n\n");
QByteArray inputData;
while(!std::cin.eof())
{
char buffer[1024];
std::cin.read(buffer, sizeof(buffer));
int amountRead = std::cin.gcount();
inputData.append(buffer, amountRead);
}
printf("{}");
QString inputString(inputData);
std::cout << inputString.toStdString() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment