By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to use Google App Script to send a file using POST request
JavaScript

Javascript – How to use Google App Script to send a file using POST request

Admin
Last updated: 2023/12/10 at 1:15 PM
Admin
Share
3 Min Read
How to use Google App Script to send a file using POST request

Problem:

I want to use Google Apps Script to integrate a Google Sheets with an API

Contents
Problem:Solution:Sample script:Note:Reference:

This is the documentation from the API service:

curl --location --request POST 'https://apinew.socialhub.pro/api/sendMessage' \
--form 'api_token="SEU API_TOKEN AQUI"' \
--form 'phone="NUMERO DESTINO"' \
--form 'file=@"DIRETÓRIO DO ARQUIVO"' \
--form 'message="MENSAGEM"'

I have manage to create a script in Google App Script that works perfectly:

function send_message_teste(telephone,message) {
  var url = "https://apinew.socialhub.pro/api/sendMessage";
  
  telephone = telephone.toString();
  
  let request = {
                  "api_token":"MY_TOKEN",
                  "phone":telephone,
                  "message":message
                };
                
  let resquest_body = JSON.stringify(request);

  let config = {
    muteHttpExceptions: true,
    method: "post",
    headers: {
      "Content-Type": "application/json"
    },
    payload:resquest_body
  };
               

  let response = UrlFetchApp.fetch(url, config);
  var data = JSON.parse(response.getContentText());
  
  return response
}

My problem is that I can not make this work with a file. The API documentation has only the curl example. I have manage to translate this example to Google App Script, but I got stuck when trying to send a file through the API. Can anyone help me?

Solution:

I believe your goal is as follows.

  • You want to convert the following curl command to Google Apps Script.

    curl --location --request POST 'https://apinew.socialhub.pro/api/sendMessage' \
    --form 'api_token="SEU API_TOKEN AQUI"' \
    --form 'phone="NUMERO DESTINO"' \
    --form 'file=@"DIRETÓRIO DO ARQUIVO"' \
    --form 'message="MENSAGEM"'
    

I thought that when I saw your curl command, the request is multipart/form-data. But, in your showing script, it seems that the value is sent ad JSON data. I thought that this might be the reason for your current issue. When this is reflected in a sample script, how about the following sample script?

Sample script:

Please set the file ID of your file on Google Drive to fileId.

function send_message_teste(telephone,message) {
  const fileId = "###"; // Please set your file ID of the file you want to upload.

  var url = "https://apinew.socialhub.pro/api/sendMessage";
  telephone = telephone.toString();
  let request = {
    "api_token": "MY_TOKEN",
    "phone": telephone,
    "message": message
  };
  const payload = { ...request, file: DriveApp.getFileById(fileId).getBlob() };
  let config = { muteHttpExceptions: true, payload };
  let response = UrlFetchApp.fetch(url, config);
  var data = JSON.parse(response.getContentText());
  return response;
}
  • When payload is included, the POST method is automatically used.

Note:

  • I think that the request of this modified script is the same with the above curl command. But, if an error occurs, please confirm your values of request and fileId, again.

  • If the structure of multipart/form-data of this modified script cannot be used in your API, this Google Apps Script library might be able to be used. Ref (Author: me)

Reference:

  • fetch(url, params)

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Not able to Parse a valid javascript Json object with " to snowflake table using parse_json [duplicate] Javascript – Not able to Parse a valid javascript Json object with \” to snowflake table using parse_json [duplicate]
Next Article ReactJS: Textbox Creating Extra Scroll at Bottom of Page Javascript – ReactJS: Textbox Creating Extra Scroll at Bottom of Page
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?