JSON issue when adding webhook headers through Cloud API

Hi, I’m trying to add custom headers to a webhook integration through the cloud API. Below is my python client code snippet.

       data = {
          "integration_type": "Webhook",
          "url": "URL",
          "event": "data_event",
          "requestType": "POST",
          "json": {},
          "headers": "{\n  \"x-api-key\": \"wFEKgW5pvtY1xsFod7******\"\n}"
      }
      response = requests.post(url, data=data)

The webhook is successfully created.

Response
{'id': '633460bfd3af831194274021', 'event': 'data_event', 'created_at': '2022-09-28T14:57:03.044Z', 'product_id': 17315, 'logs': [], 'errors': [], 'integration_type': 'Webhook', 'url': 'https://to****.execute-api.us-east-1.amazonaws.com/prod/data', 'requestType': 'POST', 'headers': '{\n  "x-api-key": "wFEKgW5pvtY1xsFod7******"\n}', 'json': '{\n  "payload": {\n    "devid": "{{{devid}}}",\n    "time": "{{{time}}}",\n    "tmscale": "{{{tmscale}}}",\n    "tmoffset": "{{{tmoffset}}}",\n }\n}', 'noDefaults': False, 'rejectUnauthorized': True, 'ok': True, 'integrationUrl': 'https://api.particle.io/v1/products/iot-bup-17315/integrations/633460bfd3af831194274021'}

However, the headers turn out like so when I verify in the console.

Could you provide any help with this? Thank you!

When creating the request like this, headers should be a JSON object not a string. For example:

data = {
          "integration_type": "Webhook",
          "url": "URL",
          "event": "data_event",
          "requestType": "POST",
          "json": {},
          "headers": {"x-api-key": "wFEKgW5pvtY1xsFod7******"}
      }
1 Like

Hi Rick, thanks for the quick response.

I tried your way, and I’m still getting the same weird result in the console.
Here is the response

{'id': '6334929c3f2ffe5c5cf4b3af', 'event': 'bud_data_event', 'created_at': '2022-09-28T18:29:48.219Z', 'product_id': 17315, 'logs': [], 'errors': [], 'integration_type': 'Webhook', 'url': 'https://tos6139k43.execute-api.us-east-1.amazonaws.com/prod/bud-data', 'requestType': 'POST', 'headers': 'x-api-key', 'json': '{\n  "payload": {\n    "devid": "{{{devid}}}",\n    "time": "{{{time}}}",\n    "tmscale": "{{{tmscale}}}",\n  }\n}', 'noDefaults': False, 'rejectUnauthorized': True, 'ok': True, 'integrationUrl': 'https://api.particle.io/v1/products/iot-bup-17315/integrations/6334929c3f2ffe5c5cf4b3af'}

It looks like the headers declaration isn’t quite right. Can you print it out:

print(json.dumps(data))

Also you have the same string not an object problem in your declaration for the json key.

print result:

{
    "integration_type": "Webhook",
    "url": "https://tos6139k43.execute-api.us-east-1.amazonaws.com/prod/bud-data",
    "event": "bud_data_event",
    "requestType": "POST",
    "json": "{\n  \"payload\": {\n    \"devid\": \"{{{devid}}}\",\n    \"time\": \"{{{time}}}\",\n    \"tmscale\": \"{{{tmscale}}}\",\n    \"tmoffset\": \"{{{tmoffset}}}\",\n    \"gnsslock\": \"{{{gnsslock}}}\",\n    \"cellsig\": \"{{{cellsig}}}\",\n    \"latitude\": \"{{{latitude}}}\",\n    \"longitude\": \"{{{longitude}}}\",\n    \"heading\": \"{{{heading}}}\",\n    \"altitude\": \"{{{alt}}}\",\n    \"inst_speed\": \"{{{inst_speed}}}\",\n    \"horz_acc\": \"{{{horz_acc}}}\",\n    \"hdop\": \"{{{hdop}}}\",\n    \"vdop\": \"{{{vdop}}}\",\n    \"vert_acc\": \"{{{vert_acc}}}\",\n    \"avg_speed\": \"{{{avgspd}}}\",\n    \"devbatt\": \"{{{devbatt}}}\",\n    \"freemem\": \"{{{freemem}}}\"\n  }\n}",
    "headers": {
        "x-api-key": "wFEKgW5pvtY1xsFod7******"
    }
}

json key works properly only when formatted as string (as shown in the console screenshot)

Do you have any other suggestions? There seems to be inconsistency with the formatting, is it possible there’s a fix on the Particle side?

Hi just wanted to bump this in case you missed my last message, thanks!