gpt4 book ai didi

Error when trying to post data to rest API using an appscript(尝试使用应用程序脚本将数据提交到REST API时出错)

翻译 作者:bug小助手 更新时间:2023-10-26 22:27:55 24 4
gpt4 key购买 nike



I am struggling to write an appscript to post data to sage accounting API

我正在努力编写将数据发布到SAGE会计API的应用程序脚本


I am trying to save a new customer to sage accounting software through the API. When I use postman it works. I cant get the appscript to work though.

我正在试着通过API拯救一个新客户到SAGE会计软件。当我使用邮递员时,它起作用了。不过,我无法让应用程序脚本正常工作。


The following is the code I am using:

以下是我使用的代码:


function postDataToSageOneAPI() {
// Define your API endpoint URL
var apiUrl = 'https://resellers.accounting.sageone.co.za/api/2.0.0/Customer/Save';

// Define authentication credentials
var username = 'myusername';
var password = 'mypassword';

// Extract the CompanyId from the URL
var companyId = "14787";

// Define the API key
var apiKey = "{####################################}";

// Create the payload data you want to send as JSON
var payload = '{"Name": "string1", "Email": "string2"}'; // Assuming this is a valid JSON string

// Add the API key and CompanyId to the URL as query parameters
apiUrl += '?apikey=' + apiKey + '&CompanyId=' + companyId;

// Define the headers for the request
var headers = {
'Authorization': 'Basic ' + Utilities.base64Encode(username + ':' + password),
'Content-Type': 'application/json',
};

// Configure the options for the request
var options = {
'method': 'POST',
'headers': headers,
'payload': payload,
};

// Make the POST request to the Sage One API
var response = UrlFetchApp.fetch(apiUrl, options);

// Check the response status code
var statusCode = response.getResponseCode();

// Handle the response
if (statusCode === 200) {
var responseBody = response.getContentText();
// Process the responseBody as needed
Logger.log('Response Body: ' + responseBody);
} else {
// Handle errors or other status codes
Logger.log('Error: ' + statusCode);
}
}

I keep getting the following error:

我一直得到以下错误:



Exception: Invalid argument:

https://resellers.accounting.sageone.co.za/api/2.0.0/Customer/Save?apikey={#####################}&CompanyId=14787
postDataToSageOneAPI @ Code.gs:35



What's the problem?

有什么问题吗?


更多回答
优秀答案推荐

I needed to do the following

我需要做以下事情


encodeURIComponent for apikey and companyid

ApiKey和Company id的encodeURIComponent


Use cases for encodeURIComponent

EncodeURIComponent的用例


User has submitted values in a form that may be in a string format and need to be passed in, such as URL fields.
Need to accept query string parameters in order to make GET requests.

用户提交了表单中的值,该表单可能是字符串格式,需要传入,例如URL字段。需要接受查询字符串参数才能发出GET请求。


here is the modified code

以下是修改后的代码


var apiUrl = 'https://resellers.accounting.sageone.co.za/api/2.0.0/Customer/Save?apikey=' + encodeURIComponent(apiKey) + '&CompanyId=' + encodeURIComponent(companyId);

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com