Skip to content

CORS issue: res.set_header() not working #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dk9966 opened this issue Dec 7, 2021 · 1 comment
Closed

CORS issue: res.set_header() not working #1118

dk9966 opened this issue Dec 7, 2021 · 1 comment

Comments

@dk9966
Copy link

dk9966 commented Dec 7, 2021

server.cc:

void runServer(Database &database)
{
    // Generate server object
    httplib::Server svr;

    //other svr.get, svr.post in between

    svr.Put("/city", [&database](const httplib::Request &req, httplib::Response &res)
            {

                // Parse request body and define repsonse object
                nlohmann::json requestBody = nlohmann::json::parse(req.body), response;

                // Ensure all required fields are present
                if (requireFields(res, requestBody, {"id", "field", "value"}, response))
                {
                    // Extract fields
                    std::string id = requestBody["id"];
                    std::string field = requestBody["field"];
                    std::string value = requestBody["value"];
                    // Call helper function
                    // Fill in response object
                    database.UpdateCity(id, field, value);
                    response["response"]["success"] = true;
                }

                // Stringify response and set as response

                res.set_header("Access-Control-Allow-Origin", "*");
                res.set_content(response.dump(), "text/json");
                database.PrintTables();
            });
}

App.js:

const printDB = (event) => {
    if (event.key === "Enter") {
      fetch("http://localhost:8080/city", {
        method: "PUT",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          id: "NOmDpk5F3Bp1ZkOvTgpe",
          field: "CITY_NAME",
          value: "nyc",
        }),
      });
    }
  };

Currently, the code in the server.cc works well using curl calls. However, when I have a client that sends a request to the server which updates the things in a database, there is a CORS issue that prevents the request from going through. I have tried using res.set_header("Access-Control-Allow-Origin", "*"); but the same error message keeps popping up. I'm not sure what I am missing at this point.
image

I have the res.set_header() line but the error message shows that it isn't detected. I've also searched in closed issues and didn't really get much help from that.

@yhirose
Copy link
Owner

yhirose commented Dec 7, 2021

Hope this helps.

@yhirose yhirose closed this as completed Dec 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants