Datarouter Advance Webhook & M2X AT-T

Hello there people of the internet,

I’m currently stuck on how

to send Payload using Hologram Advanced Webhook Builder

here are some detail:

Desitination URL: http://api-m2x.att.com/v2/devices/82d69f4c52d669f64a8c66e31afde94f/updates

Payload: payload

Headers:
Content-Type: application/json
X-M2X-KEY: 14b61b4ec1531fa7666e51dba11b83d0
-----------------------------------------------python_code------------------------------------------------------------

from urllib2 import Request, urlopen
import datetime

waktu = datetime.datetime.now().isoformat()
print (waktu + “Z”)
print (“2018-04-19T00:00:00.000Z”)

values = “”"
{
“deviceid”: 162134,
“data”:[
“payload”:{
“values”: {
“temperature”: [
{ “timestamp”: “2018-04-23T00:00:00.000Z”, “value”: 15 },
{ “timestamp”: “2018-04-24T00:00:00.000Z”, “value”: 20 },
{ “timestamp”: “2018-04-25T00:00:00.000Z”, “value”: 25 } ],
“humidity”: [
{ “timestamp”: “2018-04-23T00:00:00.000Z”, “value”: 80 },
{ “timestamp”: “2018-04-24T00:00:00.000Z”, “value”: 85 },
{ “timestamp”: “2018-04-25T00:00:00.000Z”, “value”: 80 } ]
},
“location”: { “name”: “Central Park”, “latitude”: 40.77177, “longitude”: -73.97352 , “elevation”: 0 }
}
]
}
“”"
headers = {
‘Content-Type’: ‘application/json’
}
request = Request(‘https://dashboard.hologram.io/api/1/csr/rdm?apikey=2BeHy2zvsuTyqddO0qmZvgBU84Bwk4’, data=values, headers=headers)

response_body = urlopen(request).read()
print response_body


------------------------------------------------------------------------------python_code-------------------------------------

the problem is i kept getting error says code 400, Anybody knows how the correct way is? please help

Yeah I, share them keys but don’t worry, this is purely for education purposePreformatted text

Did you receive the 400 Bad Request error when posting to Hologram’s API or from the Advanced Webhook Route (M2X responding with error 400)?

Also, use <<decdata>> as the payload value, this could possibly solve the issue. https://hologram.io/docs/guide/cloud/data-router/

FYI, your M2X and Hologram API keys are visible. Make sure to regenerate both :wink:

Greetings, thanks for your reply

I had tried to change from payload to <<decdata.payload>> but I still had this error code 400. When i view the Hologram dashboard console there’s nothing, I assume that it is from M2X server
response

Any idea how I can overcome this problem?

*don’t worry about the API key

Reviewing your code, you’re sending a string where you need to be sending JSON. Try this.

from urllib2 import Request, urlopen
import datetime
import json

waktu = datetime.datetime.now().isoformat()
print (waktu + "Z")
print ("2018-04-19T00:00:00.000Z")

values = {
  "deviceid": 162134,
  "data":[
    "payload":{
      "values": {
        "temperature": [
          { "timestamp": "2018-04-23T00:00:00.000Z", "value": 15 },
          { "timestamp": "2018-04-24T00:00:00.000Z", "value": 20 },
          { "timestamp": "2018-04-25T00:00:00.000Z", "value": 25 } 
        ],
        "humidity": [
          { "timestamp": "2018-04-23T00:00:00.000Z", "value": 80 },
          { "timestamp": "2018-04-24T00:00:00.000Z", "value": 85 },
          { "timestamp": "2018-04-25T00:00:00.000Z", "value": 80 } 
        ]
      },
      "location": { 
        "name": "Central Park", 
        "latitude": 40.77177, 
        "longitude": -73.97352 , 
        "elevation": 0 
      }
    }
  ]
}

headers = {"Content-Type": "application/json"}

request = Request("https://dashboard.hologram.io/api/1/csr/rdm?apikey=CHANGETHISYO!", data=json.dumps(values), headers=headers)

response_body = urlopen(request).read()
print response_body

Here is a Python3 snippet:

import urllib.request
import json  

body = {
    "deviceid": YOUR_DEVICE_ID,
    "data": "Hello, Hologram!"
  }

myurl = "https://dashboard.hologram.io/api/1/csr/rdm?apikey=YOUR_API_KEY"

req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')

jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('utf-8')   # needs to be bytes

req.add_header('Content-Length', len(jsondataasbytes))
print (jsondataasbytes)

response = urllib.request.urlopen(req, jsondataasbytes) 
print(response)

Greeting, thanks for your help but it seems that hologram data accept this type of data "data": "{\value\": \"100\" }" . Is there other way I can put json without having \ <— everytime I put json syntax in the payload?

Here how the coding looks like and it works, but there’s back slash:

from urllib2 import Request, urlopen
import datetime
import json

waktu = datetime.datetime.now().isoformat()
print (waktu + "Z")
print ("2018-04-19T00:00:00.000Z")

values = {
  "deviceid": 162134,
  "data": "{ \"values\": { \"temperature\": [ { \"timestamp\": \"2018-04-25T00:00:00.000Z\", \"value\": \"15\" }, { \"timestamp\": \"2018-04-26T00:00:00.000Z\", \"value\": \"20\" }, { \"timestamp\": \"2018-04-27T00:00:00.000Z\", \"value\": \"25\" } ], \"humidity\": [ { \"timestamp\": \"2018-04-25T00:00:00.000Z\", \"value\": \"80\" }, { \"timestamp\": \"2018-04-26T00:00:00.000Z\", \"value\": \"85\" }, { \"timestamp\": \"2018-04-27T00:00:00.000Z\", \"value\": \"80\" } ] }, \"location\": { \"name\": \"Central Park\", \"latitude\": \"40.77177\", \"longitude\": \"-73.97352\", \"elevation\": \"0\" } }"
}

print json.dumps(values)

headers = {"Content-Type": "application/json"}

request = Request("https://dashboard.hologram.io/api/1/csr/rdm?apikey=2BeHy2zvsuTyqddO0qmZvgBU84Bwk4", data=json.dumps(values), headers=headers)

response_body = urlopen(request).read()
print response_body

But thanks for the json format, but know I encounter a new problem. Can you give me a suggestion? Thanks in advanced :kissing_heart:

That is how python handles strings and is not a Hologram thing. In python you could use single quotes inside a double quote string.

Here is an example:

values = {
  "deviceid": 162134,
  "data": "{ 'values': { 'temperature': [ { 'timestamp': '2018-04-25T00:00:00.000Z', 'value': '15' } ] } }"
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.