Archaeopteryx Posted August 30 Posted August 30 Hi, I am a cloud software engineer and I have built hundreds of REST APIs for private and public use. Recently, I created an Email REST API on my private network. It is very simple, and it works from many REST testing tools such as Postman. I have other private REST APIs that I have built that capture data from the Shelly device, they use HTTP.POST. And the data capture APIs work fine with Shelly. The HTTP.POST sends the payload in the request and my mongodb gets the data. However, for my Email API, it uses the same HTTP.POST and same server, but that request never reaches my server, and it is the same server the data APIs are on. Shelly returns 200 success, but the server API logs show that it never was requested. I update the firmware to the latest (1.4.2) and hoped that would resolve it, but it did not. I have tried for hours, but it seems Shelly says it sent the request, but my server never saw it. I have run out of ideas on how to resolve this. I need this email notification to work, the whole point for this Shelly device was to monitor a piece of equipment that runs late at night. We needed to be notified when it actually ran. Thanks in advance! Archaeopteryx *************************************************** Here is the log (note: I changed the email address and apikey for privacy): shos_rpc_inst.c:242 switch.toggle via WS_in 192.168.0.193:55328 05:37:13 shelly_notification:162 Status change of switch:0: {"id":0,"output":false,"source":"WS_in"} 05:37:13 shelly_ejs_rpc.cpp:41 Shelly.call HTTP.POST {"url":"http://192.168.0.3/SendMailApi?time=1725021433034.0390625","body":"{\"email\":{\"to\":\"test@gmail.com\",\"subject\":\"Message From Shelly\",\"text\":\"This is a test body\"}}","headers":{"Content-Type":"application/json","X-Api-Key":"zzzzzzzzzzzzzzzzzzzzz"}} 05:37:13 shos_rpc_inst.c:242 HTTP.POST via loopback 05:37:13 shelly_http_client.:304 0x3ffe861c: HTTP POST http://192.168.0.3/SendMailApi?time=1725021433034.0390625 05:37:13 shelly_http_client.:607 0x3ffe861c: Finished; bytes 650, code 200, redir 0/3, auth 0, status OK 05:37:13 Email sent successfully to http://192.168.0.3/SendMailApi 05:37:13 Data Sent: {"email":{"to":"test@gmail.com","subject":"Message From Shelly","html":"This is a test body"}} ***************************************************** The source code below: //Email_test let CONFIG = { outputId: 0, }; let mail_api = "http://192.168.0.3/SendMailApi"; let outputStringId = "switch:" + JSON.stringify(CONFIG.outputId); Shelly.addStatusHandler(function (statusNtf) { if (statusNtf.component === outputStringId) { send_email_to_api({ "email": { "to": "test@gmail.com", "subject": "Message From Shelly", "html": "This is a test body" } }); } }); function send_email_to_api(msg) { let strNow = new Date(); Shelly.call( "HTTP.POST", { url: mail_api + "?time=" + strNow, body: JSON.stringify(msg), headers: { "Content-Type": "application/json", "X-Api-Key": "zzzzzzzzzzzzzzzzzzzzz", "Cache-Control": "no-cache" } }, function (response, error_code, error_message) { if (error_code === 0) { print("Email sent successfully to " + mail_api + "?time=" + strNow); print("Data Sent: " + JSON.stringify(msg)); } else { print("Failed to send email. Error code:", error_code, "Message:", error_message); } } ); } Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish
Archaeopteryx Posted August 30 Author Posted August 30 (edited) It is resolved. I realized that the header object in my request is ignored. The API Key was in the header, but it was not making it to the endpoint via the relay server. So the request was getting rejected due to a missing api key. I added the apikey to the body, and was able to parse it during the server receive, so now it makes it to the final endpoint. It was my bad, I misunderstood that Shelly HTTP.POST does not have a "headers" object. At least, in this thread, I have provided an example source code to call an email API so that others can benefit 🙂 Enjoy! -Archaeopteryx Edited August 30 by Archaeopteryx Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.