HTTP POST a JSON-formatted

Posted on
Sun Oct 06, 2019 1:34 pm
RAID5 offline
Posts: 104
Joined: Jan 16, 2015
Location: Sweden

HTTP POST a JSON-formatted

Hi
I'm a beginner to Python and need some assistance.
I have used a plugin for a long time named Beacon and now it has stopped working because of my App on my phone suddenly stopped working.
The reason only wanted to use this app is that it post from the provider servers and I could open IPs from the provider and not the whole internet.
Other apps post from the phone and I don't want that.
I now have a temporary solution but need to create a Python script to post to the plugin while I create a new solution.

I have following command in my app when posting and need to replicate it in Python code in indigo instead.
http://server.nu:123/?sender=my.name@gm ... vent=enter

More information about the Beacon plugin here:
http://www.indigodomo.com/pluginstore/121/
https://github.com/IndigoDomotics/Beacon/

RAID5 "A happy Swede addicted to automation"

Posted on
Sun Oct 06, 2019 3:37 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: HTTP POST a JSON-formatted

Check out https://requests.kennethreitz.org/en/master/

Code: Select all
import requests

req = requests.get('http://server.nu:123/?sender=my.name@gmail.com&location=home&event=enter')

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Oct 06, 2019 9:56 pm
RAID5 offline
Posts: 104
Joined: Jan 16, 2015
Location: Sweden

Re: HTTP POST a JSON-formatted

Hi

Thanks for your fast reply

I tried your code with a small change, I need to make a post instead. So I changed to post in the command.
But it didn't work. I got following error message
Beacon Error Exception: cannot concatenate 'str' and 'NoneType' objects

import requests

req = requests.post('http://server.nu:123/?sender=my.name@gmail.com&location=home&event=enter')

RAID5 "A happy Swede addicted to automation"

Posted on
Mon Oct 07, 2019 12:11 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: HTTP POST a JSON-formatted

Is that your only code in this script?

Because there’s no concatenation taking place in that code to cause that error, that I can see?


Sent from my iPhone using Tapatalk Pro

Posted on
Mon Oct 07, 2019 6:14 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: HTTP POST a JSON-formatted

A POST is slightly different.

Code: Select all
requests.post('http://server.nu:123/', data = {'sender':'my.name@gmail.com', 'location':'home', 'event':'enter'})
Last edited by FlyingDiver on Mon Oct 07, 2019 11:55 am, edited 1 time in total.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Oct 07, 2019 11:12 am
RAID5 offline
Posts: 104
Joined: Jan 16, 2015
Location: Sweden

Re: HTTP POST a JSON-formatted

Now i got this error instead.
Beacon Error Didn't recognise received data. (User-agent: python-requests/2.10.0, Content-type: application/x-www-form-urlencoded)

RAID5 "A happy Swede addicted to automation"

Posted on
Mon Oct 07, 2019 11:17 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: HTTP POST a JSON-formatted

Do you have an example of a successful call to that URL from a command line using curl? Or anything else? That's correct for the data you gave me, but I can't be sure that was complete.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Oct 07, 2019 11:28 am
RAID5 offline
Posts: 104
Joined: Jan 16, 2015
Location: Sweden

Re: HTTP POST a JSON-formatted

Image
Here is a snapshot from a similar application called Locative

Sending a test from this App results in this in the log in Indgo
Beacon Enter location notification received from sender/location ff0c7684-0aa0-4cea-a7b0-01cd2c9c0abf@@test

I think it's using JSON to POST but I'm not sure.


Skickat från min iPhone med Tapatalk

RAID5 "A happy Swede addicted to automation"

Posted on
Mon Oct 07, 2019 11:56 am
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: HTTP POST a JSON-formatted

There was a typo in the code I posted. Try using this fixed version:

Code: Select all
requests.post('http://server.nu:123/', data = {'sender':'my.name@gmail.com', 'location':'home', 'event':'enter'})

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Oct 07, 2019 12:41 pm
RAID5 offline
Posts: 104
Joined: Jan 16, 2015
Location: Sweden

Re: HTTP POST a JSON-formatted

If you meant the typo next to event, I spotted that and corrected it before.


Skickat från min iPhone med Tapatalk

RAID5 "A happy Swede addicted to automation"

Posted on
Mon Oct 07, 2019 12:48 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: HTTP POST a JSON-formatted

Well, that's how you do it in Python. Once you figure out what the correct parameters are.

If you think it's actually expecting a JSON payload, you might want this:

Code: Select all
requests.post('http://server.nu:123/', json={'sender':'my.name@gmail.com', 'location':'home', 'event':'enter'})

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Oct 07, 2019 2:19 pm
RAID5 offline
Posts: 104
Joined: Jan 16, 2015
Location: Sweden

Re: HTTP POST a JSON-formatted

FlyingDiver wrote:
Well, that's how you do it in Python. Once you figure out what the correct parameters are.

If you think it's actually expecting a JSON payload, you might want this:

Code: Select all
requests.post('http://server.nu:123/', json={'sender':'my.name@gmail.com', 'location':'home', 'event':'enter'})



YES you are the man!!!
I was close, so close, I have missed the forward slash after the port number.
Thanks alot!

RAID5 "A happy Swede addicted to automation"

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 17 guests