Difference between revisions of "Receive JSON POST Data"
Jump to navigation
Jump to search
Michael.mast (talk | contribs) |
Michael.mast (talk | contribs) |
||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Purpose== | ==Purpose== | ||
| − | Request came across my desk for a web server to receive JSON POST data, providing web hooks for | + | Request came across my desk for a web server to receive JSON POST data, providing web hooks for another system to parse. On my end things were simple enough. |
#Simple Apache server to receive requests. | #Simple Apache server to receive requests. | ||
#Basic Authentication | #Basic Authentication | ||
#Save the POST data to file (For now, but I feel this will be expanded in the near future) | #Save the POST data to file (For now, but I feel this will be expanded in the near future) | ||
| + | |||
==Process== | ==Process== | ||
| + | ===Apache Config=== | ||
| + | Very simple and clean vhost. | ||
| + | <pre> | ||
| + | <VirtualHost *:80> | ||
| + | DocumentRoot /var/www/html/ | ||
| + | <Directory /var/www/html/> | ||
| + | AuthType Basic | ||
| + | AuthName "Restricted" | ||
| + | AuthBasicProvider file | ||
| + | AuthUserFile "/etc/httpd/password" | ||
| + | Require valid-user | ||
| + | </Directory> | ||
| + | </VirtualHost> | ||
| + | </pre> | ||
| + | Quick and dirty password file | ||
| + | <pre> | ||
| + | htpasswd -bc /etc/httpd/password test test | ||
| + | </pre> | ||
| + | |||
| + | ===PHP Script=== | ||
<ref>https://stackoverflow.com/questions/16931804/recording-a-json-post-to-file-using-php</ref><ref>https://stackoverflow.com/questions/18866571/receive-json-post-with-php</ref>This example will echo the posted data back to the client during testing, as well as save the file to disk. | <ref>https://stackoverflow.com/questions/16931804/recording-a-json-post-to-file-using-php</ref><ref>https://stackoverflow.com/questions/18866571/receive-json-post-with-php</ref>This example will echo the posted data back to the client during testing, as well as save the file to disk. | ||
<pre> | <pre> | ||
| Line 20: | Line 41: | ||
$myFile = "testFile_$today.txt"; | $myFile = "testFile_$today.txt"; | ||
$data = file_get_contents('php://input'); | $data = file_get_contents('php://input'); | ||
| − | |||
file_put_contents($myFile,$data); | file_put_contents($myFile,$data); | ||
| − | |||
?> | ?> | ||
| + | </pre> | ||
| + | Another way of doing this would be the following<ref>https://gist.github.com/techslides/5df05f62f2a034ee537c</ref> | ||
| + | <pre> | ||
| + | <?php | ||
| + | $today = date("mdY_His"); | ||
| + | $file = "file_$today"; | ||
| + | $dir = '/var/www/html/recevied/'; | ||
| + | $full = $dir.$file.'.txt'; | ||
| + | echo $full; | ||
| + | $data = file_get_contents('php://input'); | ||
| + | $forwriting = fopen($full, "w"); | ||
| + | fwrite($forwriting, $data); | ||
| + | fclose($forwriting); | ||
| + | ?> | ||
| + | |||
| + | </pre> | ||
| + | |||
| + | ==Testing== | ||
| + | Used the following POST method with curl to test with. | ||
| + | <pre> | ||
| + | curl -X POST -H "Content-Type: application/json" --data '{"test":"1"}' http://localhost | ||
| + | </pre> | ||
| + | Again, but with a username/password<ref>https://stackoverflow.com/questions/3044315/how-to-set-the-authorization-header-using-curl</ref> | ||
| + | <pre> | ||
| + | curl --user test:test -X POST -H "Content-Type: application/json" --data '{"test":"1"}' http://localhost | ||
| + | </pre> | ||
| + | ==Final Config== | ||
| + | Not finished, for reference. | ||
| + | <pre> | ||
| + | yum install httpd php python2-certbot-apache | ||
| + | htpasswd -Bc /etc/httpd/password user | ||
| + | |||
| + | cat << EOF >> /etc/httpd/conf.d/vhost.conf | ||
| + | <VirtualHost *:80> | ||
| + | DocumentRoot /var/www/html/ | ||
| + | <Directory /var/www/html/> | ||
| + | AuthType Basic | ||
| + | AuthName "Restricted" | ||
| + | AuthBasicProvider file | ||
| + | AuthUserFile "/etc/httpd/password" | ||
| + | Require valid-user | ||
| + | </Directory> | ||
| + | </VirtualHost> | ||
| + | EOF | ||
| + | |||
| + | certbot --apache | ||
| + | |||
| + | mv /etc/httpd/conf.d/ssl.conf ./ | ||
| + | touch /etc/httpd/conf.d/ssl.conf | ||
| + | echo "Listen 443" >> /etc/httpd/conf.d/vhost-le-ssl.conf | ||
| + | systemctl restart httpd | ||
</pre> | </pre> | ||
Latest revision as of 09:20, 1 August 2018
Purpose
Request came across my desk for a web server to receive JSON POST data, providing web hooks for another system to parse. On my end things were simple enough.
- Simple Apache server to receive requests.
- Basic Authentication
- Save the POST data to file (For now, but I feel this will be expanded in the near future)
Process
Apache Config
Very simple and clean vhost.
<VirtualHost *:80> DocumentRoot /var/www/html/ <Directory /var/www/html/> AuthType Basic AuthName "Restricted" AuthBasicProvider file AuthUserFile "/etc/httpd/password" Require valid-user </Directory> </VirtualHost>
Quick and dirty password file
htpasswd -bc /etc/httpd/password test test
PHP Script
[1][2]This example will echo the posted data back to the client during testing, as well as save the file to disk.
<?php
$myFile = "testFile.txt";
$data = file_get_contents('php://input');
echo $data;
file_put_contents($myFile,$data);
?>
To further build on this, and to keep track of the received hooks, I wanted to add a date/time stamp to the file[3].
<?php
$today = date("mdY_His");
$myFile = "testFile_$today.txt";
$data = file_get_contents('php://input');
file_put_contents($myFile,$data);
?>
Another way of doing this would be the following[4]
<?php
$today = date("mdY_His");
$file = "file_$today";
$dir = '/var/www/html/recevied/';
$full = $dir.$file.'.txt';
echo $full;
$data = file_get_contents('php://input');
$forwriting = fopen($full, "w");
fwrite($forwriting, $data);
fclose($forwriting);
?>
Testing
Used the following POST method with curl to test with.
curl -X POST -H "Content-Type: application/json" --data '{"test":"1"}' http://localhost
Again, but with a username/password[5]
curl --user test:test -X POST -H "Content-Type: application/json" --data '{"test":"1"}' http://localhost
Final Config
Not finished, for reference.
yum install httpd php python2-certbot-apache htpasswd -Bc /etc/httpd/password user cat << EOF >> /etc/httpd/conf.d/vhost.conf <VirtualHost *:80> DocumentRoot /var/www/html/ <Directory /var/www/html/> AuthType Basic AuthName "Restricted" AuthBasicProvider file AuthUserFile "/etc/httpd/password" Require valid-user </Directory> </VirtualHost> EOF certbot --apache mv /etc/httpd/conf.d/ssl.conf ./ touch /etc/httpd/conf.d/ssl.conf echo "Listen 443" >> /etc/httpd/conf.d/vhost-le-ssl.conf systemctl restart httpd
- ↑ https://stackoverflow.com/questions/16931804/recording-a-json-post-to-file-using-php
- ↑ https://stackoverflow.com/questions/18866571/receive-json-post-with-php
- ↑ http://php.net/manual/en/function.date.php
- ↑ https://gist.github.com/techslides/5df05f62f2a034ee537c
- ↑ https://stackoverflow.com/questions/3044315/how-to-set-the-authorization-header-using-curl