How to accept JSON POST?

For the first time it says here that this is concrete ... So, here it goes.

I am doing a small project to link Salesforce with my Raspberry Pi. The goal is to make light (think of a lighthouse, a lilac light) when a high priority event comes in the Salesforce customer service. Currently, customers usually send an email to a specific address, and this creates a case. He goes to the "Unallocated queue" and sends an email to the team in which this case is awaiting appointment.

Salesforce uses REST, so I need Pi to accept JSON so that it can easily understand what Salesforce wants to do.

Currently, I think I have won half the battle. I have a web server (Lighttpd) running on a Pi that contains an index page and a Python script. I also use the Python shell, which allows me to easily run a command from the Telldus program that I installed. This program controls the USB radio transmitter that I connected, it is paired with an RF connector that is connected to a power source with the light connected to it.

So, the Python script is called power.py and can be controlled using URL variables, so if I go to power.py?device=1&power=on&time=10&password=hunter2, which turns on device 1 for 10 seconds. I also created a POST form on the index page, which simply adds POSTS to the python script and runs it the same way as URL variables. This all works great.

So all I have to do is connect it to Salesforce. I would like to use REST and JSON, so if I ever switch from Salesforce to another CRM program, it can easily adapt and get instructions from new places.

I published a Python script that I use here: https://github.com/7ewis/RemotePiControl/blob/master/power.py

Currently Pi is not allowed from the local network, so I need to somehow develop a way to send JSON commands, and also get and convert them to work using the correct variables, etc. I am not a programmer, I am simply exposed to languages ​​from hacking things and learning. Therefore, why do I need to be guided by this.

I have never used REST or JSON before, so what do I need to do to achieve this?

+4
2

-, Python script, CGI. .

, ​​ Flask. - ,

from flask import Flask

application = Flask(__name__)

@application.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # use Flask build in json decoder
        the_data = request.get_json()
        # then do something with the data
        return "This was a POST request, how interesting..."
    else:
        # request was GET rather than POST, so do something with else
        return "Hello World!"

, Flask Lighttpd http://flask.pocoo.org/docs/deploying/fastcgi/

, Python script JSON ( Python http://www.python-requests.org/en/latest/), , HTTP, HTTPRequester Firefox (https://addons.mozilla.org/en-US/firefox/addon/httprequester/)

0

Source: https://habr.com/ru/post/1529427/


All Articles