Please, what have I done wrong? I’m getting error message “Unexpected end of JSON input”
export async function sendSMS(telephone, message) {
const mySecret = await getSecret("BurstSMS");
var url = "https://api.transmitsms.com/send-sms.json?message=" + message + "&to=" + telephone + "&from=CAKEARTISTS"
const headers = {
Accept:'application/json',
Authorization:mySecret
};
const request = {
method: 'GET',
headers: headers
};
return fetch(url, request)
.then(response => response.json())
.then(json => console.log(JSON.stringify(json),
)
)
.catch((error) => {
console.log('Oops... ' + error);
});
}
This code appears to be console.log
-ing the JSON and then returning nothing. At least that’s what I think has gone wrong.
return fetch(url, request)
.then(response => response.json())
.then(json => console.log(JSON.stringify(json),))
.catch((error) => {
console.log('Oops... ' + error);
});
If the desire is to return the response.json()
call then try removing this line: .then(json => console.log(JSON.stringify(json),))
Also for clarity you could rewrite this code to something like like:
let response = await fetch(url, request)
let json = await response.json();
return json;
If there is an error it should still log to console.error()
and be visible in site event logs so the console.log()
isn’t necessary.
I tried that. Thanks. Have written and rewritten in so many ways and I just can’t get it to send the SMS.
Current version results in: ERROR - Internal Server Error
import { fetch } from 'wix-fetch';
import {getSecret} from 'wix-secrets-backend';
export async function sendSMS(telephone, message) {
const mySecret = await getSecret("BurstSMS");
const url = `https://api.transmitsms.com/send-sms.json?message=${message}&to=${telephone}&from=CakeArtists`;
const headers = {
Accept: "application/json",
Authorization: mySecret,
// Host: "api.transmitsms.com",
};
const request = {
method: "GET",
headers: headers,
};
let response = await fetch(url, request);
if (!response.ok) {
throw new Error(`Failed to send SMS: ${response.statusText}`);
}
let json = await response.json();
if (!json) {
throw new Error("Failed to parse JSON response");
}
return json;
}
Have read through all the API docs from BurstSMS and come up with no reason for it not to work.
Appreciate any input you might have.
I gave up with BurstSMS and am using ClickSend instead.
1 Like