Skip to content
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

webhook generates empty post #186

Closed
victorclaessen opened this issue Apr 25, 2020 · 3 comments
Closed

webhook generates empty post #186

victorclaessen opened this issue Apr 25, 2020 · 3 comments
Assignees
Labels

Comments

@victorclaessen
Copy link

emqx 4.0.6

I am trying to get webhooks working, but am having an issue. Perhaps I do not understand what exactly I have to do. The documentation is not sufficient for me.

I have been able to make emqx republish data on another mqtt topic using rules, but not post it to a webhook. I will explain the steps I have taken.

Using the EMQ dashboard, I make a rule with the SQL "SELECT payload FROM "t/#". I then add an action handler, and select "Data to Web Server" using a previously configured resource (POST to http://localhost/webhook.php, empty request header list). I leave the Payload Template field empty.

I have the following webhook.php:

<?php
file_put_contents("log/post.log", print_r($_POST, true)); 
print "ok";
?>

I then post to topic t/test a string value of test. I can then see in the apache log that emqx has posted to webhook.php. The file log/post.log has been made. Unfortunately, the contents of the file is the PHP representation of an empty array, i.e. the POST was empty.

I was hoping for things like clientid, topic and payload to have been posted.

Can you suggest what I am doing wrong?

Thanks!

@HJianBo
Copy link
Member

HJianBo commented Apr 26, 2020

Hi, @victorclaessen Is your message going to be big? There is a BUG in v4.0.6 that causes a large message not to be encoded as JSON. see emqx/emqx#3382

Please you can try the v4.1-alpha.2 version. It may be fixed it.

@HJianBo HJianBo self-assigned this Apr 26, 2020
@victorclaessen
Copy link
Author

victorclaessen commented Apr 26, 2020

In my case, the message payload was short (the string value "test"), so I'm not sure if the bug affects this situation.

Could you possibly comment on my test strategy? Am I doing things right generally?

I will try to get the alpha version. (edit) I have looked at the alpha version of the plugin, but I'm not sure how to compile it. Do you have instructions for compiling, or a binary for Debian 10/amd64?

@victorclaessen
Copy link
Author

I'm sorry. My understanding of parsing POST data in PHP was wrong. EMQ is posting all the variables as json. PHP just doesn't get json data like it does HTML form data (as explained here). When I changed my webhook.php to the following, I got my json formatted data:

<?php
$name=$_SERVER[UNIQUE_ID];
file_put_contents("webhooklog/post.$name.log", file_get_contents("php://input"));
print "ok";
?>

The data in the log file was:

{"username":"undefined","topic":"t/test","timestamp":1587891259502,"qos":0,"publish_received_at":1587891259502,"peerh
ost":"127.0.0.1","payload":"TEST","node":"emqx@REDACTEDHOSTNAME","msg":"TEST","id":"5A42DBE76EE02A1
D6490031280001","headers":{"username":"undefined","protocol":"mqtt","proto_ver":4,"peerhost":"127.0.0.1"},"flags":{"r
etain":false,"dup":false},"event":"message.publish","clientid":"mosq-SplOK4c5Z2Fgr1HWCZ"}

I will now go on to try and figure out how to format the json data. I did not find anything about the formatting in the documentation. If I cannot get it to work, I will open a new issue.

Sorry to bother you with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants