I am running IoT Hub sample project on ESP32 device using Azure IoT Device SDK for C. It's returning the following response for a direct method when I am calling it from Azure portal:
"Successfully invoke device method"
However, we are using the Node SDK's invokeDeviceMethod() of azure-iothub to invoke the direct method. I get an exception on this method with the following error string:
'Unexpected token in JSON at position 70'
As per my research, C direct method is not sending data is proper json format which is required by node APIs.
https://github.com/Azure/azure-iot-sdk-node/issues/736
Kindly help!
Can you detail what sample you are using or how you are using the API's?
Thanks @danewalton for your quick reply
I have setup ESP32 Device as per the steps mentioned in article below to integrate with Azure IoT Hub:
https://docs.microsoft.com/en-us/samples/azure-samples/esp32-iot-devkit-get-started/sample/
And I am using NodeJS BackEndApplication.js file to call the direct method as mentioned in this link:
https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-control-device-node#call-the-direct-method
Apart from connection strings and device details, there are no other coding changes done in these examples.
Please let me know if any additional information is needed from my end.
Are you able to verify using the IoT Explorer what the message is that comes in?
Getting exactly the same error as I was getting in NodeJs application:
Error: Unexpected token in JSON at position 70
What is the message you are sending back from the method? At least from what I can tell with the sample, the response would be valid JSON.
I have not changed the response message. it's exactly the same which is mentioned in example:
const char *responseMessage = "\"Successfully invoke device method\"";
Interesting....the error says unexpected token at position 70 but the message is only 33 characters long? And if I put it in that wrapper from the other issue:
"{"status":200,"payload":{"Successfully invoke device method"}}"
that's only 62 characters long. Can you post the raw data that the hub is receiving? So in the NodeJS case print out what the application receives before parsing the doc.
Just a small correction. The error is happening at position no 59 with default response message.
Response Message:
const char *responseMessage = "\"Successfully invoke device method\"";
Error:
Failed to invoke method on device ESP32***: Error: Unexpected token in JSON at position 59
Ah okay here's what's happening. So
"Successfully invoke device method"
is valid JSON by itself. This would parse out correctly. Now underneath the covers, this value is getting wrapped in the
{"status":200,"payload":{"Successfully invoke device method"}}
wrapper. This is no longer valid JSON. Try changing the *responseMessage
to
\"value\":\"Successfully invoked device method\"
Hi @danewalton
I already tried all these options, but it doesn't work.
Here is the error I am getting when passing the *responseMessage as the string you mentioned:
{"message":"{\"Message\":\"{\\"errorCode\\":502101,\\"trackingId\\":\\"0b205c42b5f94fef980fab9e7bf12f9a-G:21-TimeStamp:09/05/2020 09:50:22-G:9-TimeStamp:09/05/2020 09:50:22\\",\\"message\\":\\"Payload is not a valid document of type application/json.\\",\\"info\\":{},\\"timestampUtc\\":\\"2020-09-05T09:50:22.6747425Z\\"}\",\"ExceptionMessage\":\"\"}"}
JFYI...I am getting this error in Azure Portal as well when JSON structure is not correct.
As per my understanding the default *responseMessage message structure is correct. However, the issue is happening because somehow C SDK is sending the response message string in single quote instead of double quotes, which is not a valid JSON format.
Please suggest.
Damn that's weird. So the esp32 "wrapper" code seems to be living here:
https://github.com/VSChina/ESP32_AzureIoT_Arduino
They do a wrapping around our SDK that could be doing some unintended stuff. I'll have to get a board to try this out for myself so might take a bit of time. Thanks for your patience here.
Thanks for the update
Hi @danewalton
JFYI...I tried the steps mentioned in below git repo, but getting exactly the same error:
https://github.com/VSChina/ESP32_AzureIoT_Arduino
@danewalton : Any update on this?
Hi @harikeshydv,
I haven't been able to find an answer yet but I would suggest you issue a GH issue on the above repo (https://github.com/VSChina/ESP32_AzureIoT_Arduino) since it seems to be a bug in their code. If I find some new info I will post it here.
Oh wait I got it.
So debugging the service side, here is what it looks like:

Everything looks to be okay but copying the value of the string over looks like this
"{\"status\":200,\"payload\":{\"payload\":\"Successfully invoke device method\"}\u0000}"
So it seems a NULL character is getting tacked on. Looking at the device code, here's the culprit:
*response_size = strlen(responseMessage) + 1;
*response = (unsigned char *)strdup(responseMessage);
The response size should be the length of the string NOT including the NULL. So change it to
*response_size = strlen(responseMessage);
*response = (unsigned char *)strdup(responseMessage);
Thanks @danewalton. It's working fine now.
@danewalton, @harikeshydv, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey
Had exactly the same problem. You saved me a lot of time. Thanks
Most helpful comment
Had exactly the same problem. You saved me a lot of time. Thanks