Please help: can't post JSON data to emoncms

Hi,

Please, can someone help me?

I’m trying to upload some DS18B20 temperature data to emoncms but the JSON string generate is incorrect, as follows:

https://emoncms.org/input/post.json=%7BBed1:?24.6%7D&apikey=429xxxxxxxxxxxxxxxxxxx1c52cd609

It should have looked lik this, instead:

    https://emoncms.org/input/post.json={Bed1:?24.6}&apikey=429xxxxxxxxxxxxxxxxxxx1c52cd609

So, essentially the curly braces are being replaced in the Python code. I do understand why: those are used by Python for other operations. But I don’t know how to fix it? I did try some google searches for an answer but couldn’t find any.

How do I format my URL’s to be compliant with the emoncms API? i.e; Emoncms - site api

   r = requests.post("https://emoncms.org/input/post.json={Bed1:", params=str(myTempCase) + "}&apikey="+APIKEY)
  print(r.url)
  r.status_code == requests.codes.ok
  print(r.status_code)

This should help (not used it in a while). Quickstart — Requests 2.26.0 documentation

I’m not entirely sure POST requests are supported at emoncms.org, you could try using a GET request like this

r = requests.get(https://emoncms.org/input/post.json?node=1&json={Bed1:24.6}&apikey=429xxxxxxxxxxxxxxxxxxx1c52cd609)

(note I have added a node id too)

or there is further POST api documentation on the local emoncms installs at a similar url, which can also be viewed in the source code on the github repo

Hi Brian,

Thanx, but I honestly don’t know what to look for in that manual you gave me?

The problem lies with the curley braces - i.e. the { and } Python uses the curly braces as part of it’s own structure, and emoncms.org expects is as part of the URL.
The other URL options doesn’t allow me to post directly to a given feed

So, the question is, how do I pass a curly brace to my URL structure, without Python thinking it should perform some form of regex call?

i.e. in PHP when you want to “escape” certain characters, you put a forward slash in front of it, but that doesn’t work in Python.

I thought the example was straight forward (works for post as well). I know, using a GET is counter intuative :slight_smile:

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)

for

http://httpbin.org/get?key2=value2&key1=value1

Your construct is not correct (the url part should end after ‘post.json’), and then construct the data part.

Long time since I last used it though…

A requests.post expects 2 args, the first is a url and the second is the payload in the form of a python dict (dictionary), this is where the curly braces are used.

payload = {'key1': 'value1', 'key2': 'value2'}

As Brian points out your construct is incorrect, you have part of a url containing one curly brace as the first arg and then pass a single value concatenated with the remainder of the url as a string, as the second in place of the dict.

A python dict is very similar to JSON and you’re supposed to pass the dict (JSON?) as an object into the requests.post in the form params = payload where payload is a JSON like object, not a string representation wrapped in curly braces.

You must include the apikey and node id as part of the url as a near complete api call, only the actual key:value data pairs will be placed in the “payload”.

You don’t pass the braces to a requests.post, but you should be able to pass them to a requests.get as that expects a single arg in the form of a complete url containing the JSON string which would include the curly braces.

None of the input api calls will allow you to post direct to a feed. that would require a feed api call, you do not need to use a POST to post data direct to an input, “counter intuitive” as Brian says but pretty much the norm around here.

I have had a peek at the code and it does look like POST is implemented on emoncms.org despite the absence of documentation on the input api docs page so you should be able to use POST, but GET may be easier.

The PHP escape character is a backslash i.e. \ not /
Ref: PHP: Escape sequences - Manual

Thanx borpin. I understand what you’re saying, but how do I get the format expect on Emoncms - site api?