Upload Image through HTTP POST Request

Hi,

In one my apps I am using PUSHOVER api to send notifications. I am using HTTP POST for the API. Its working fine. But now i have a requirement to send an image along with the HTTP POST request. Is it possible. The image should be actual byte data and not url of image. Can I upload the actual binary data of image within the JSON content. Below is the documentation given by them but it is not for JSON format. Can a similar thing be done within JSON or any other method within appsheet

Below is what i normally use inside the JSON Body. I need to add attachment in this code. Can someone help me with this.

{
“token”: “asdfghjkl”,
“user”: “lkjhgfdsa”,
“title”:“<<[Title]>>”,
“message”: “<<[Body]>>”,
“sound”:“bike”,
“url”:“<<“https://www.appsheet.com/start/123456789”&[DeepLink]>>”,
“url_title”:“View Full Details”
}

1 Like

You can easily do it via appscript. You need to create I think blob oject. Attached as one of the parameters.

1 Like

Hi @AngeloParana Thanks for the reply. But how do i get the image from appsheet to appscript. I tried the API separately with form-data body and image is sending successfully. Is there a similar way possible in JSON body.

Hello @jyothis_m You may check on this thread how to get the file lin

https://www.googlecloudcommunity.com/gc/Announcements/Call-to-Action-for-Apps-Constructing-URLs-to-GetTableFileUrl/td-p/359650

Then use Urlfetchapp to pass the value like this

3 Likes

Thank you @AngeloParana Shall try this

Hi @AngeloParana I tried but didn’t succeed. I have very little knowledge in coding, Below is the code i am using in appscript, can you please guide me to implement it. The notification is reaching correctly but no attachment. Can you help me where I am going wrong.

function Pushover(){
// Make a POST request with a JSON payload.
var data = {
‘token’: ‘asdfghjkl’,
‘user’: ‘lkjhgfdsa’,
‘title’:‘Image Notification Test’,
‘message’: ‘Test Message’,
‘sound’:‘Bike’,
‘attachment’:{
‘filename’:‘test.jpg’,
‘Content-Type’:‘image/jpeg’,
‘data’: UrlFetchApp.fetch(“https://play-lh.googleusercontent.com/V_P-I-UENK93ahkQgOWel8X8yFxjhOOfMAZjxXrqp311Gm_RBtlDXHLQhwFZN8n4aIQ=w240-h480-rw”).getBlob()
}
};
var options = {
‘method’ : ‘post’,
‘contentType’: ‘application/json’,
// Convert the JavaScript object to a JSON string.
‘payload’ : JSON.stringify(data)
};
UrlFetchApp.fetch(‘https://api.pushover.net/1/messages.json’, options);
}

Hello @jyothis_m . Tried to create a set up and successfully made it.

Here are the details

Here is the formula to get the image link from virtual columm

CONCATENATE( “https://www.appsheet.com/template/gettablefileurl”,“appName=”,ENCODEURL(CONTEXT(“AppName”)), “&tableName=”,ENCODEURL(CONTEXT(“Table”)),“&fileName=”,ENCODEURL([Image]))

App must disable to security setting in order to fetch the image

Here is log of image link successfully fetch

Create an automation to appscript

Here is the appscript code

function form_data(e) {

var youtubeLogoUrl =e;

Logger.log(e)

var file = UrlFetchApp

.fetch(youtubeLogoUrl)

.getBlob()

.setName(“youtubeLogoBlob”);

var url = “https://api.pushover.net/1/messages.json”;

var form = {

token : “apri2xcbvwnbstrzsssu85tosq9i33”,

user : “ucw7i37vs8c65bbc4d9nxnv698esj4”,

device : “21091116ag”,

title : “Backup finished - SQL1”,

message : " Backup of database example finished in 16 minutes.",

attachment : file

};

send_pushover(url,form);

}

function send_pushover(url,form) {

var options = {

method : “POST”,

payload : form

};

var response = UrlFetchApp.fetch(url,options);

Logger.log(response)

}

Test result

Hope you understand it.

5 Likes

You are Amazing!!! :handshake: @AngeloParana It worked flawlessly. Thanks a lot for your help. I was struggling with the code needed in GAS. Thanks once again. :hugs:

2 Likes