Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,272 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am trying to initialise file upload referring to this documentation with C++, but I am getting an error
"errorCode":400004,"trackingId":"7e49d9f8-2XXX-XXXX-XXXX-XXXXXXXXXXX6","message":"Invalid idScope length","timestampUtc":"2024-09-16T07:05:13.903829Z"
My code
// Function to perform the HTTPS POST request for file upload
std::string initialiseFileUpload(const std::string &certificate, const std::string &key, const std::string &fileNameWithFolder, const std::string &assignedHub, const std::string ®istrationId) {
std::cout << "Starting HTTPS POST request for file upload..." << std::endl;
// Decode the base64-encoded P12 certificate
std::string p12_data = DecodeBase64(certificate);
std::cout << "P12 certificate decoded. Size: " << p12_data.size() << " bytes." << std::endl;
EVP_PKEY *pkey = nullptr;
X509 *cert = nullptr;
STACK_OF(X509) *ca = nullptr;
// Set up SSL context and load the P12 certificate
SSL_CTX *ctx = setup_ssl_context(p12_data, key, pkey, cert, ca);
if (!ctx) {
return "";
}
SSL *ssl;
BIO *bio_conn;
std::string bioAddress = assignedHub + ":443";
bio_conn = BIO_new_ssl_connect(ctx);
BIO_get_ssl(bio_conn, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(bio_conn, bioAddress.c_str());
if (BIO_do_connect(bio_conn) <= 0) {
ERR_print_errors_fp(stderr);
std::cerr << "Failed to connect to server." << std::endl;
BIO_free_all(bio_conn);
SSL_CTX_free(ctx);
return "";
}
std::cout << "Connected to server." << std::endl;
std::string url_path = "/devices/" + registrationId + "/files?api-version=2021-04-12";
std::string jsonBody = "{\"blobName\": \"" + fileNameWithFolder + "\"}";
std::string request = "POST " + url_path + " HTTP/1.1\r\n"
"Host: " + assignedHub + "\r\n"
"Content-Type: application/json\r\n"
"Content-Length: " + std::to_string(jsonBody.size()) + "\r\n"
"Connection: close\r\n\r\n" + jsonBody;
BIO_write(bio_conn, request.c_str(), request.length());
std::cout << "HTTP POST request sent." << std::endl;
char buffer[1024];
std::string response;
while (int bytesRead = BIO_read(bio_conn, buffer, sizeof(buffer) - 1)) {
if (bytesRead <= 0) break;
buffer[bytesRead] = '\0';
response += buffer;
}
std::cout << "Response received. Size: " << response.size() << " bytes." << std::endl;
BIO_free_all(bio_conn);
SSL_CTX_free(ctx);
EVP_PKEY_free(pkey);
X509_free(cert);
sk_X509_pop_free(ca, X509_free);
EVP_cleanup();
std::cout << "Cleaned up OpenSSL resources." << std::endl;
// Find the start of the JSON body by looking for the first '{'
std::size_t jsonStart = response.find('{');
// Find the end of the JSON body by looking for the last '}'
std::size_t jsonEnd = response.rfind('}');
std::string jsonResponse;
if (jsonStart != std::string::npos && jsonEnd != std::string::npos) {
// Extract the JSON portion of the response
jsonResponse = response.substr(jsonStart, jsonEnd - jsonStart + 1);
std::cout << "Trimmed JSON response: " << jsonResponse << std::endl;
} else {
std::cout << "JSON response not found." << std::endl;
}
return jsonResponse;
}