如何使用 GET 方法访问 RESTlet SuiteScript 参数

Zac*_*han 2 javascript rest netsuite

我被困在一个可能很多新的 SuiteScript黑客都会遇到的问题上。

正如SuiteScript p.的官方文档所写的那样。243,有这个 JS 用于使用 GET 方法检索记录。

// Get a standard NetSuite record
function getRecord(datain) {
    return nlapiLoadRecord(datain.recordtype, datain.id); // e.g recordtype="customer", id="769"
}

//  http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769
Run Code Online (Sandbox Code Playgroud)

但是,当我在 NetSuite 端尝试EXACT片段时,datain.recordtype未定义。(并且返回应该只返回文本,顺便说一句。)

幸运的是,我自己找到了解决方案。在下面检查我的答案。

Zac*_*han 5

在这个片段中(与上面相同)......

function getRecord(datain) {
    return nlapiLoadRecord(datain.recordtype, datain.id); // e.g recordtype="customer", id="769"
}

//  http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769
Run Code Online (Sandbox Code Playgroud)

SuiteScriptdatain不是作为对象或 JSON填充,而是作为字符串填充(因为我仍然忽略的原因。)

您要做的只是在解析它之前,然后使用点符号访问 JSON。

function getRecord(datain) {
    var data = JSON.parse(datain); // <- this
    return "This record is a " + data.recordtype + " and the ID is " + data.id;
}

//  http://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=22&deploy=1&recordtype=customer&id=769
Run Code Online (Sandbox Code Playgroud)

我已经更改了解决方案中的 return 语句,因为当我尝试返回不是文本的内容时,SuiteScript 给了我错误。

或者

正如egrubaugh360所说,在您的查询脚本中指定Content-Typeis application/json(调用您的 SuiteScript 脚本的那个)

所以如果你像我一样处理 Node.js,它会给出这样的东西:

var options = {
    headers: {
        'Authorization': "<insert your NLAuth Authentification method here>",
        "Content-Type" : "application/json" // <- this
    }
}

https.request(options, function(results) {
    // do something with results.
}
Run Code Online (Sandbox Code Playgroud)

希望这会帮助某人。

  • 我相信这完全取决于您首先向 RESTlet 发送的请求类型。如果你发送一个带有 `text/plain` 的 `Content-Type` 的请求,那么是的,`datain` 将是一个字符串。如果你发送一个`application/json` 请求,那么你*应该*得到一个对象。这是 RESTlet 仅有的两个可接受的 `Content-Types`(不幸的是)。 (2认同)