How to see the response status code from the node.js Cumulocity web sdk other than from .core.fetch method

I can’t seem to see the response status code for calls made to Cumulocity when carrying out a number of operations such as creating a measurement, alarm, event and updating an inventory managed object.

For example, to create a measurement, I have written the following:

const { data, res } = await c8yClients.measurement.create(measurementBody);

However when I observe the data and res values from a successful call, I am unable to obtain the status code 201 as defined in the API documentation here: Cumulocity - OpenAPI

I know that we can obtain the status code when using the .core.fetch method in the web sdk as follows:

const res = await c8yClients.core.fetch(URL,...)

But as you can see, it is using the .core.fetch. of the web sdk, for which I can obtain the status in the response via res.status. Is this the only way to get the response status code?

I just tried this out myself and I can get the res.status from the client.measurement.create() function returns the HTTP status code of the API request.

Maybe you’re running into some other issue with the code.

Below is a snippet that I used:

async function main() {
  const auth = new BearerAuth(process.env["C8Y_TOKEN"]);
  let baseUrl = process.env["C8Y_HOST"];
  if (!baseUrl?.startsWith("http://") && !baseUrl?.startsWith("https://")) {
    baseUrl = `https://${baseUrl}`;
  }
  const client = new Client(auth, baseUrl);
  const mandantoryObject = {
    sourceId: 3449597721,
    fragment: { series: { unit: "%", value: 51 } }
  };
  const { data, res } = await client.measurement.create(mandantoryObject);
  console.log("response", {status: res.status});
}
(async () => {
  await main();
})();

Output

response { status: 201 }
2 Likes

Thanks Reuben. You example helped!