Send certificate & Key file in Rest Api

shahanshah_alam_e5655bc6d

Shahanshah Alam

Posted on July 16, 2024

Send certificate & Key file in Rest Api

Hi
I want to do certificate based authentication in React Native, for that I am using Rest API.
And from the docs https://learn.microsoft.com/en-us/azure/iot-dps/how-to-control-access#certificate-based-authentication I found out this cURL, and I need to send certificate and key file in API, as https.agent is not supported by React Native, how can I manage to do that?

curl -L -i -X PUT –cert ./[device_cert].pem –key ./[device_cert_private_key].pem -H 'Content-Type: application/json' -H 'Content-Encoding:  utf-8' -d '{"registrationId": "[registration_id]"}' https://global.azure-devices-provisioning.net/[ID_Scope]/registrations/[registration_id]/register?api-version=2021-06-01
Enter fullscreen mode Exit fullscreen mode

So far what I have tried is

 const data = {
    registrationId: registrationId,
  };
  const cert = await RNFS.readFile(certificatePath, 'utf8');
  const key = await RNFS.readFile(keyPath, 'utf8');
  console.log('cert', cert);
  console.log('key', key);
  try {
    const httpsAgent = new https.Agent({
      cert: cert,
      key: key,
      keepAlive: true,
    });
    console.log('httpsAgent', httpsAgent);
    const response = await axios({
      method: 'PUT',
      url: `https://global.azure-devices-provisioning.net/${scopeId}/registrations/${registrationId}/register?api-version=2021-06-01`,
      data: data,
      httpsAgent: httpsAgent,
      headers: {
        'Content-Type': 'application/json',
        'Content-Encoding': 'utf-8',
      },
    });
    return response.data;
  } catch (err) {
    console.log('err', err);
    return err;
  }
Enter fullscreen mode Exit fullscreen mode

Thank you!!!

💖 💪 🙅 🚩
shahanshah_alam_e5655bc6d
Shahanshah Alam

Posted on July 16, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024