简体   繁体   English

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

[英]PUT request - google script url fetch issue

I'm having issues to use the put function.我在使用 put 函数时遇到问题。 Anyone can find out the error?任何人都可以找出错误? Thank you谢谢

CURL --卷曲——

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"
          }
     ]
}
'

My google script:我的谷歌脚本:

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);

  };

I'm trying to push data using the PUT request from google script, GET is working, but can't figure out with PUT, how to implement it with google urlfetch我正在尝试使用来自 google 脚本的 PUT 请求推送数据,GET 正在工作,但无法弄清楚 PUT,如何使用 google urlfetch 实现它

From your showing sample curl command, how about the following modification?从您显示的示例 curl 命令中,进行以下修改如何?

Modified script:修改脚本:

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);
}

Note:笔记:

  • In this modification, it supposes that your token and the values of the request body are valid values.在此修改中,它假设您的令牌和请求正文的值是有效值。 If an error related to them occurs, please confirm them again.如果出现与它们相关的错误,请再次确认它们。

  • From your error message, I noticed that it is required to modify the value of request body.从您的错误消息中,我注意到需要修改请求正文的值。 So, I reflected in it.所以,我反思了一下。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM