question

Kamron Clark avatar image
Kamron Clark asked Kamron Clark published

Request to create custom user greeting returns socket hang up error

I am attempting to migrate the part of my app for creating a custom user greeting from using a local file to using a file publicly hosted online. Here is my current code:


async function create_greeting() {
  try {
    let FormData = require('form-data');
    formData = new FormData();
    let body = {
        type: "Introductory",
        answeringRule: {id: "3839201036"}
    }
 
    formData.append('json', new Buffer.from(JSON.stringify(body)), {
          filename: 'request.json',
          contentType: 'application/json'
          });
 
    formData.append('binary', fs.createReadStream('URL_to_file'));
    let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
    let jsonObj = await resp.json()
      .then(() => console.log(JSON.stringify(jsonObj)));
  } catch(e) {
    console.log(e.message)
  }
}

When I changed the file reference on line 15 from a local file path to a public URL I got the following response: "request to https://platform.ringcentral.com/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true failed, reason: socket hang up". How do I change the function to use a web-hosted file? Language is Node.js and I am using Google Cloud Storage to host the file.

rest apicustom greeting
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Phong Vu avatar image
Phong Vu answered Kamron Clark published

Google does not allow accessing its Cloud Storage that way. Ask Google support or search on the stack overflow such as this one to learn how to download or read to a stream.

https://stackoverflow.com/questions/71097537/how-to-download-file-from-google-cloud-storage

5 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

After referencing that article and the Google Cloud Storage documentation I can't get the create greeting endpoint to accept the data downloaded into memory. For the purposes of this app, downloading to a file is not an option. Here is my current code:

                    
  1. async function downloadIntoMemory() {
  2. // Downloads the file into a buffer in memory.
  3. const contents = await storage.bucket(bucketName).file(destFileName).download();
  4.  
  5. console.log(
  6. `Contents have been downloaded`
  7. );
  8.  
  9. //console.log(Buffer.from(contents.toString()).toString());
  10. create_greeting(contents.toString('binary'));
  11. }
  12.  
  13. async function create_greeting(data) {
  14. try {
  15. let FormData = require('form-data');
  16. formData = new FormData();
  17. let body = {
  18. type: "Introductory",
  19. answeringRule: { id: "3839201036"}
  20. }
  21. formData.append('json', new Buffer.from(JSON.stringify(body)), {
  22. filename: 'request.json',
  23. contentType: 'application/json'
  24. });
  25. formData.append('binary', fs.createReadStream(data));
  26. let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
  27. let jsonObj = await resp.json()
  28. .then(() => console.log(JSON.stringify(jsonObj)));
  29. } catch(e) {
  30. console.log(e.message)
  31. }
  32. }
  33.  
  34. downloadIntoMemory().catch(console.error);

When I run this code I get the error: "The argument 'path' must be a string or Uint8Array without null bytes". How can I change the data buffer from the Cloud Storage download into an acceptable format?

0 Likes 0 ·

I am not an expert on GC and I don't have an account to try it. But this may help

https://stackoverflow.com/questions/28256033/load-a-gcloud-createreadstream-into-a-variable-in-nodejs

0 Likes 0 ·

Is it possible to pass raw data into the request for the "binary" parameter? I've tried passing the audio as a Buffer, arrayBuffer, base64 string, Blob, and Blob string and none of them have worked. Almost all of them fail with the message "Invalid attachment media type". How come using the CreateReadStream() method on a local file works for passing the audio, but passing the binary of that file doesn't?

0 Likes 0 ·

After reviewing the article and the Google Cloud Storage documentation, I can't get the FormData to accept the data downloaded into a buffer. I've tried the raw buffer data, converting to string, converting to binary, and I can't get it to accept the data format. For the purposes of this app, downloading to a file is not an option. Here is my current code:


async function downloadIntoMemory() {
  // Downloads the file into a buffer in memory.
  const contents = await storage.bucket(bucketName).file(destFileName).download();

  console.log(
    `Contents have been downloaded`
  );

  //console.log(Buffer.from(contents.toString()).toString());
  create_greeting(contents.toString('binary'));
}

async function create_greeting(data) {
    try {
      let FormData = require('form-data');
      formData = new FormData();
      let body = {
          type: "Introductory",
          answeringRule: {id: "3839201036"}
      }
   
      formData.append('json', new Buffer.from(JSON.stringify(body)), {
            filename: 'request.json',
            contentType: 'application/json'
            });
   
      formData.append('binary', data);
      let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
      let jsonObj = await resp.json()
        .then(() => console.log(JSON.stringify(jsonObj)));
    } catch(e) {
      console.log(e.message)
    }
}


When I run this code, I get the error: "Invalid attachment media type". When I run the code but with fs.createReadStream(data) instead, I get this error: "The argument 'path' must be a string or Uint8Array without null bytes. Received 'ID3\x04\x00\x00\x00\x00\x00\x1CTSSE\x00\x00\x00\b\x00\x00\x03Google\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00D\x00\x00\x00...". How can I get it to accept the data type without creating a file?

0 Likes 0 ·

After referencing that article and the Google Cloud Storage documentation I can't get the create greeting endpoint to accept the data downloaded into memory. For the purposes of this app, downloading to a file is not an option. Here is my current code:

                    
  1. async function downloadIntoMemory() {
  2. // Downloads the file into a buffer in memory.
  3. const contents = await storage.bucket(bucketName).file(destFileName).download();
  4.  
  5. console.log(
  6. `Contents have been downloaded`
  7. );
  8.  
  9. //console.log(Buffer.from(contents.toString()).toString());
  10. create_greeting(contents.toString('binary'));
  11. }
  12.  
  13. async function create_greeting(data) {
  14. try {
  15. let FormData = require('form-data');
  16. formData = new FormData();
  17. let body = {
  18. type: "Introductory",
  19. answeringRule: { id: "3839201036"}
  20. }
  21. formData.append('json', new Buffer.from(JSON.stringify(body)), {
  22. filename: 'request.json',
  23. contentType: 'application/json'
  24. });
  25. formData.append('binary', fs.createReadStream(data));
  26. let resp = await platform.post('/restapi/v1.0/account/~/extension/2896037036/greeting?apply=true', formData);
  27. let jsonObj = await resp.json()
  28. .then(() => console.log(JSON.stringify(jsonObj)));
  29. } catch(e) {
  30. console.log(e.message)
  31. }
  32. }
  33.  
  34. downloadIntoMemory().catch(console.error);

When I run this code I get the error: "The argument 'path' must be a string or Uint8Array without null bytes. Received 'ID3\x04\x00\x00\x00\x00\x00\x1CTSSE\x00\x00\x00\b\x00\x00\x03Google\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00D\x00\x00\x00...". How can I change the data buffer from the Cloud Storage download into an acceptable format?

0 Likes 0 ·

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys