2

I have tried to save a json which has the following json with a special character 'ø'.

The json is {"username":"Jøhn"}.

I have used this api to save the json in azure blob ==> https://${storageAccountName}.blob.core.windows.net/${containerName}/${name}${sasToken}

The json saved in the blob container is {"username":"Jøhn" (LAST CURLY BRACE IS MISSING).

The headers used in rest api is : 'x-ms-blob-type': 'BlockBlob', 'x-ms-date': date, 'x-ms-version': '2016-05-31', 'Content-Type': 'text/plain', 'Content-Length': value.length

The code is :

const date = (new Date()).toUTCString();

const sasToken = await Storage.GenerateSasTokenIfExpired();

const endpoint = `https://${storageAccountName}.blob.core.windows.net/${containerName}/${name}${sasToken}`;

return backOff(() => new Promise((resolve, reject) => {
  request.put({
    'body': value,
    'headers': {
      'x-ms-blob-type': 'BlockBlob',
      'x-ms-date': date,
      'x-ms-version': '2016-05-31',
      'Content-Type': 'text/plain',
      'Content-Length': value.length
    },
    'url': endpoint
  }, function (err, result) {
    if (err) {
      return reject(err);
    }
    if (result.statusCode !== 201) {
      return reject(result.body);
    }
    return resolve(result);
  });
}), AzureBackOff.retryPolicy);

1 Answers1

0

Your suspicion is correct. Basically the issue is coming because of value.length of a string containing special character (ø).

When I ran the following code:

const value = '{"username":"Jøhn"}';
console.log(value);
console.log('value.length', value.length);//Prints 19
const buffer = Buffer.from(value);
console.log('buffer length', buffer.length);//Prints 20

Because the content-length is being passed as 19, one less character is sent and that's why you're seeing this issue.

Please change the following line of code:

'Content-Length': value.length

to

'Content-Length': Buffer.from(value).length

and that should fix the problem.

Gaurav Mantri
  • 100,555
  • 11
  • 158
  • 192