what is request and response in this Script and How did it work?

  ajax, c++, javascript

I don’t know much about the web and Ajax, I read the Sunshine project, This code gives PIN from the user and check it , If PIN was correct get accsess.
This is Script in pin.html file :

<script>
  document.querySelector("#form").addEventListener("submit", (e) => {
    e.preventDefault();
    let pin = document.querySelector("#pin-input").value;
    document.querySelector("#status").innerHTML = "";
    let b = JSON.stringify({ pin: pin });
    fetch("/api/pin", { method: "POST", body: b })
      .then((response) => response.json())
      .then((response) => {
        if (response.status) {
          document.querySelector(
            "#status"
          ).innerHTML = `<div class="alert alert-success" role="alert">Success! Please check Moonlight to continue</div>`;
        } else {
          document.querySelector(
            "#status"
          ).innerHTML = `<div class="alert alert-danger" role="alert">PIN does not match, please check if it's typed correctly</div>`;
        }
      });
  });
</script>

This script sends one response and request. and In c++ in This part of code gives data from that script.

void savePin(resp_https_t response, req_https_t request) {
  if(!authenticate(response, request)) return;

  print_req(request);

  std::stringstream ss;
  ss << request->content.rdbuf();

  pt::ptree inputTree, outputTree;

  auto g = util::fail_guard([&]() {
    std::ostringstream data;
    pt::write_json(data, outputTree);
    response->write(data.str());
  });

  try {
    //TODO: Input Validation
    pt::read_json(ss, inputTree);
    std::string pin = inputTree.get<std::string>("pin");
    outputTree.put("status", nvhttp::pin(pin));
  }
  catch(std::exception &e) {
    BOOST_LOG(warning) << "SavePin: "sv << e.what();
    outputTree.put("status", false);
    outputTree.put("error", e.what());
    return;
  }
}

now, My question is that how can I directly add Script data to savePin function for example with PIN 1234, and In fact what is actually the request and response?

Thanks

Source: Windows Questions C++

LEAVE A COMMENT