From fb4ffea4161c3ac88eeb43bc886fab63dfdc3891 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 23 May 2022 15:27:49 +0200 Subject: feat(minion): add response headers --- minion/src/gymnasiearbete.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'minion/src/gymnasiearbete.cpp') diff --git a/minion/src/gymnasiearbete.cpp b/minion/src/gymnasiearbete.cpp index a7f1e31..5c4f725 100644 --- a/minion/src/gymnasiearbete.cpp +++ b/minion/src/gymnasiearbete.cpp @@ -116,6 +116,12 @@ void loop() const auto temperature_sensor_status = temperature_sensor.read_temperature(); + const char *response_headers[] = { "Access-Control-Allow-Origin: *", + "Content-Type: application/json" }; + + const auto response_header_cnt = + sizeof(response_headers) / sizeof(response_headers[0]); + if (temperature_sensor_status != TemperatureSensorStatus::OK) { Serial.print("Error: "); @@ -124,6 +130,8 @@ void loop() wifi_module.send_response( connection, HTTP_RESPONSE_STATUS_INTERNAL_SERVER_ERROR, + response_headers, + response_header_cnt, R"({"error": "Internal server error"})" ); @@ -137,14 +145,19 @@ void loop() snprintf( response_data, RESPONSE_DATA_MAX_LENGTH, - "{ %s: { %s: %u } }", + "{ %s: { %s: %u} }", R"("data")", R"("temperature")", temperature_sensor.temperature() ); - const auto send_response_ok = - wifi_module.send_response(connection, HTTP_RESPONSE_STATUS_OK, response_data); + const auto send_response_ok = wifi_module.send_response( + connection, + HTTP_RESPONSE_STATUS_OK, + response_headers, + response_header_cnt, + response_data + ); if (!send_response_ok) { -- cgit v1.2.3-18-g5258