From 8539ce93c466f8e840168f79656fe7cafc4ea720 Mon Sep 17 00:00:00 2001 From: ender503 Date: Wed, 6 Mar 2019 04:15:21 +0800 Subject: [PATCH 1/2] fix: Correct response Content-Type header According to the W3C specification, the media type header should be "Content-Type". See-also: https://tools.ietf.org/html/rfc7231#section-3.1.1.5 --- accelerator/server.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/accelerator/server.cc b/accelerator/server.cc index d3bdfbf2..8163b75c 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -43,7 +43,7 @@ int main(int, char const**) { api_find_transactions_by_tag(&service, req.params["tag"].c_str(), &json_result); - res.set_header("content-type", "application/json"); + res.set_header("Content-Type", "application/json"); res << json_result; }); @@ -64,7 +64,7 @@ int main(int, char const**) { api_get_transaction_object(&service, req.params["tx"].c_str(), &json_result); - res.set_header("content-type", "application/json"); + res.set_header("Content-Type", "application/json"); res << json_result; }); @@ -85,7 +85,7 @@ int main(int, char const**) { api_find_transactions_obj_by_tag(&service, req.params["tag"].c_str(), &json_result); - res.set_header("content-type", "application/json"); + res.set_header("Content-Type", "application/json"); res << json_result; }); @@ -103,7 +103,7 @@ int main(int, char const**) { char* json_result; api_get_tips_pair(&service, &json_result); - res.set_header("content-type", "application/json"); + res.set_header("Content-Type", "application/json"); res << json_result; }); @@ -121,7 +121,7 @@ int main(int, char const**) { char* json_result; api_get_tips(&service, &json_result); - res.set_header("content-type", "application/json"); + res.set_header("Content-Type", "application/json"); res << json_result; }); @@ -139,7 +139,7 @@ int main(int, char const**) { char* json_result; api_generate_address(&service, &json_result); - res.set_header("content-type", "application/json"); + res.set_header("Content-Type", "application/json"); res << json_result; }); @@ -169,7 +169,7 @@ int main(int, char const**) { api_send_transfer(&service, req.body().c_str(), &json_result); } - res.set_header("content-type", "application/json"); + res.set_header("Content-Type", "application/json"); res << json_result; }); @@ -190,7 +190,7 @@ int main(int, char const**) { const char* json = cJSON_PrintUnformatted(json_obj); res.set_status(SC_BAD_REQUEST); - res.set_header("content-type", "application/json"); + res.set_header("Content-Type", "application/json"); res << json; cJSON_Delete(json_obj); From 968f59a74a534b0155313329ec3c2ace6d68ffb9 Mon Sep 17 00:00:00 2001 From: ender503 Date: Wed, 6 Mar 2019 04:40:09 +0800 Subject: [PATCH 2/2] fix: Add response header to allow CORS requests Since API does not respond with "Access-Control-Allow-Origin" header, the response data are blocked when calling the API from browser. To eliminate this problem, add "Access-Control-Allow-Origin" header to each API response. --- accelerator/server.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/accelerator/server.cc b/accelerator/server.cc index 8163b75c..06be7940 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -44,6 +44,7 @@ int main(int, char const**) { api_find_transactions_by_tag(&service, req.params["tag"].c_str(), &json_result); res.set_header("Content-Type", "application/json"); + res.set_header("Access-Control-Allow-Origin", "*"); res << json_result; }); @@ -65,6 +66,7 @@ int main(int, char const**) { api_get_transaction_object(&service, req.params["tx"].c_str(), &json_result); res.set_header("Content-Type", "application/json"); + res.set_header("Access-Control-Allow-Origin", "*"); res << json_result; }); @@ -86,6 +88,7 @@ int main(int, char const**) { api_find_transactions_obj_by_tag(&service, req.params["tag"].c_str(), &json_result); res.set_header("Content-Type", "application/json"); + res.set_header("Access-Control-Allow-Origin", "*"); res << json_result; }); @@ -104,6 +107,7 @@ int main(int, char const**) { api_get_tips_pair(&service, &json_result); res.set_header("Content-Type", "application/json"); + res.set_header("Access-Control-Allow-Origin", "*"); res << json_result; }); @@ -122,6 +126,7 @@ int main(int, char const**) { api_get_tips(&service, &json_result); res.set_header("Content-Type", "application/json"); + res.set_header("Access-Control-Allow-Origin", "*"); res << json_result; }); @@ -140,6 +145,7 @@ int main(int, char const**) { api_generate_address(&service, &json_result); res.set_header("Content-Type", "application/json"); + res.set_header("Access-Control-Allow-Origin", "*"); res << json_result; }); @@ -170,6 +176,7 @@ int main(int, char const**) { } res.set_header("Content-Type", "application/json"); + res.set_header("Access-Control-Allow-Origin", "*"); res << json_result; }); @@ -191,6 +198,7 @@ int main(int, char const**) { res.set_status(SC_BAD_REQUEST); res.set_header("Content-Type", "application/json"); + res.set_header("Access-Control-Allow-Origin", "*"); res << json; cJSON_Delete(json_obj);