Webhook event numbering

Hi guys.
I’m working on a webhook that returns a JSON response from an API. The response is too large to fit into one transmission and I’m therefore getting “/0”, “/1” and so forth. No problem there.
The response is varying in size, so I was wondering if there is any way to get the total size/number of responses? So my program can act accordingly.

Mathias

The parts should all be 512 bytes, except for the last one, which will be whatever is left. There is no indication of the number of parts.

  • If the response part is < 512 bytes, it must be the last part.
  • If the total response is a multiple of 512 bytes, there’s no way to know for sure that it’s the end, unless the data itself can indicate it (JSON, XML). A timeout could be used, but it’s not ideal.
  • Parts may arrive out of order.
  • There is no way to request a missed part.
  • At the CoAP level, there are three attempts to deliver the packet over a 20 second period, so a response could be delayed for up to 20 seconds.
  • Using mustache templates in the hook-response can reduce the data size if you only need a subset of the data returned by the server, and the data is in JSON format.

Since your data is JSON, the best solution is to accumulate all of the parts in a buffer. Parse the number after the last slash (/0, /1) and store the data at that * 512 in the buffer. After each fragment, attempt to JSON parse the buffer until valid.

If the data varies dramatically in size, if you don’t get any fragments for around 30 seconds and you don’t have valid JSON, you probably want to discard the whole buffer as a fragment was lost.

https://docs.particle.io/reference/cloud-apis/webhooks/#multipart-responses

1 Like

Thank you for a very in depth response. Much appreciated!
Good point on JSON parsing. I’ll give that a go.

Mathias

1 Like