我正在使用PHP-curl调用REST调用,以完成official doc所引用的设备通知消息。下面是我的代码
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://MYHUBNAME.azure-devices.net/devices/MYDVCID/messages/deviceBound/DVCTAG?api-version=2018-06-30",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Authorization: MYSASTOKEN",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: MYHUBHOST",
"UserAgent: Microsoft.Azure.Devices/1.17.2",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
"content-length: "
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
在响应中,我总是收到以下JSON响应
{
"Message": "{"errorCode":400004,"trackingId":"XXXXXXXXX-G:7-TimeStamp:XXXXXXXX","message":"BadRequest","timestampUtc":"XXXXXXXXX"}",
"ExceptionMessage": ""
}
我做了很多尝试来解决它,但是没有解决问题,请帮助我
答案 0 :(得分:0)
您似乎在请求中使用了无效的ETAG值。我只是使用Postman同时使用了有效和无效的ETAG值而感到疲倦-使用无效的ETAG值,我会看到相同的错误消息:
{
"Message": "{\"errorCode\":400004,\"trackingId\":\"XXXXXXXXXXXXXXXXX-G:19-TimeStamp:05/17/2019 09:16:20\",\"message\":\"BadRequest\",\"timestampUtc\":\"2019-05-17T09:16:20.4954666Z\"}",
"ExceptionMessage": ""
}
使用有效的ETAG,我看到返回了预期的204状态。我从“接收设备绑定通知” GET请求获得的响应标头中复制了ETAG值。
这是我使用的邮递员文件:
{
"info": {
"_postman_id": "f978a27b-0ad0-4e08-a38f-2749f4cd5fcb",
"name": "IoT Hub interactions",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Receive Device Bound Notification",
"request": {
"method": "GET",
"header": [
{
"key": "Authorization",
"value": "SharedAccessSignature sr=XXXXXXX",
"description": "Generated using VS Code extension",
"type": "text"
}
],
"url": {
"raw": "https://MYHUB.azure-devices.net/devices/MyTestDevice/messages/deviceBound?api-version=2018-06-30",
"protocol": "https",
"host": [
"MYHUB",
"azure-devices",
"net"
],
"path": [
"devices",
"MyTestDevice",
"messages",
"deviceBound"
],
"query": [
{
"key": "api-version",
"value": "2018-06-30"
}
]
},
"description": "See: https://docs.microsoft.com/en-us/rest/api/iothub/device/receivedeviceboundnotification"
},
"response": []
},
{
"name": "Complete Device Bound Notification",
"request": {
"method": "DELETE",
"header": [
{
"description": "Generated using VS Code extension",
"key": "Authorization",
"type": "text",
"value": "SharedAccessSignature sr=XXXXXXXX"
}
],
"url": {
"raw": "https://MYHUB.azure-devices.net/devices/MyTestDevice/messages/deviceBound/{etag}?api-version=2018-06-30",
"protocol": "https",
"host": [
"MYHUB",
"azure-devices",
"net"
],
"path": [
"devices",
"MyTestDevice",
"messages",
"deviceBound",
"{etag}"
],
"query": [
{
"key": "api-version",
"value": "2018-06-30"
}
]
},
"description": "See: https://docs.microsoft.com/en-us/rest/api/iothub/device/completedeviceboundnotification"
},
"response": []
}
]
}