PUT请求-谷歌脚本url获取问题



我在使用put函数时遇到问题。有人能找出错误吗?谢谢你

旋度——

curl --request PUT 
--url https://open-api.guesty.com/v1/listings/XXXXXX/custom-fields 
--header 'accept: application/json' 
--header 'authorization: BEARERXXX' 
--header 'content-type: application/json' 
--data '
{
"customFields": [
{
"fieldId": "5f744b491af840002ca636a2",
"value": "Door code"
}
]
}
'

我的google脚本:

function putlistingcustomfield (){

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('get_token');// You can change the sheets name here to match your needs
var code = sheet.getRange("A18").getValue();
const myHeaders2 = {
Accept: 'application/json',
Authorization: code,
};
var data = {
'customFields': {
'fieldId': '639cdb92e128c3002a11b186',
'value': 'test'
},

};
const options = {
method: 'PUT', 
headers: myHeaders2,
followRedirects: true,
payload: JSON.stringify(data),
};
const url = 'https://open-api.guesty.com/v1/listings/XXXXXX/custom-fields';
let response2 = UrlFetchApp.fetch(url, options);
Logger.log(response2);
var sheet2 = ss.getSheetByName('write');// You can change the sheets name here to match your needs
var cell2 = sheet2.getRange("A10");
cell2.setValue(response2);
};

我正试图使用谷歌脚本的PUT请求来推送数据,GET正在工作,但无法找出PUT,如何实现它与谷歌urlfetch

从您展示的示例curl命令中,如何进行以下修改?

修改脚本:

function putlistingcustomfield() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('get_token');
var code = sheet.getRange("A18").getValue();
const myHeaders2 = {
Accept: 'application/json',
Authorization: code, // Here, the value of `code` is required to be `Bearer XXX`. Please be careful about this.
};
var data = { 'customFields': [{ 'fieldId': '639cdb92e128c3002a11b186', 'value': 'test' }] }; // Modified
const options = {
method: 'PUT',
headers: myHeaders2,
// followRedirects: true,
payload: JSON.stringify(data),
contentType: 'application/json', // Modified
};
const url = 'https://open-api.guesty.com/v1/listings/XXXXXX/custom-fields';
let response2 = UrlFetchApp.fetch(url, options);
Logger.log(response2);
var sheet2 = ss.getSheetByName('write');
var cell2 = sheet2.getRange("A10");
cell2.setValue(response2);
}

注意:

  • 在此修改中,它假设您的令牌和请求体的值是有效值。如果出现与之相关的错误,请重新确认。

  • 从你的错误信息中,我注意到需要修改请求体的值。于是,我反思在其中。

  • 参考:

  • 获取(url, params)

最新更新