My coding knowledge is pretty shaky because I didn't learn it orderly. Right now, I am trying to send an cURL Request and the documentation is:
curl https://api.at356.com/studio/v1/sd-large/complete
-H 'Content-Type: application/json'
-H 'Authorization: Bearer YOUR_API_KEY'
-X POST
-d '{"prompt": "Life is like",
"numResults": 1,
}'
this is the code
function searchFor(query) {
// Base URL to access
var urlTemplate = "https://api.at356.com/studio/v1/sd-large/complete "
// Script-specific credentials & search engine
var ApiKey = "shwMCMgEviwfz6X7Rvbtna6";
var prompt = {
"prompt": query,
"numResults": 1,
}
// Build custom cURL
var cURL = {
'method': 'POST',
"headers":{
"Content-type": "application/json",
"Authorization": ApiKey,
},
prompt,
"muteHttpExceptions": true,
"followRedirects": true,
"validateHttpsCertificates": true
};
// Perform Request
//Logger.log( UrlFetchApp.getRequest(urlTemplate, params) ); // Log query to be sent
var response = UrlFetchApp.fetch(urlTemplate, cURL,);
var respCode = response.getResponseCode();
if (respCode !== 200) {
throw new Error("Error " + respCode + " " + response.getContentText());
} else {
// Successful search, log & return results
var result = JSON.parse(response.getContentText());
Logger.log("Obtained %s search results in %s seconds.",
result.searchInformation.formattedTotalResults,
result.searchInformation.formattedSearchTime);
return result;
}
}
Can anyone tell me what I'm doing wrong and how to fix it
I believe your goal is as follows.
You want to convert the following curl command to Google Apps Script.
curl https://api.at356.com/studio/v1/sd-large/complete \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-X POST \
-d '{"prompt": "Life is like", "numResults": 1, }'
You have already confirmed that your this curl command works fine.
var query = "###"; // Please set the value.
var ApiKey = "###"; // Please set your API key.
var url = "https://api.at356.com/studio/v1/sd-large/complete";
var prompt = {
"prompt": query,
"numResults": 1,
};
var params = {
"method": "POST",
"headers": { "Authorization": "Bearer " + ApiKey },
"contentType": "application/json",
"payload": JSON.stringify(prompt),
"muteHttpExceptions": true,
};
var response = UrlFetchApp.fetch(url, params);
console.log(response.getContentText());
Authorization
is like -H 'Authorization: Bearer YOUR_API_KEY'
. So I modified to "headers": { "Authorization": "Bearer " + ApiKey }
. If your actual API key is like Bearer ###
, please modify this to "headers": { "Authorization": ApiKey }
.