我正在尝试使用模板发送信封。我使用表单数据来捕获此调用所需的所有字段。我创建了下面的对象,然后追加添加来自用户的值。然后在发送到http post之前通过JSON.stringify发送。由于某种原因仍然会收到错误INVALID_BODY_REQUEST
var data = [
{
"documents": [
{
"documentId": "27730895",
"fileExtension": "pdf",
"name": "Top Level Staging",
"order": "1"
}
],
"emailSubject": "Please Sign document",
"status": "sent",
"templateId": "4fda9b93-9c82-4f23-bd06-67f9b35f0183",
"templateRoles": [
{
"email": "",
"name": "",
"roleName": "Client",
"tabs": {
"textTabs": [
{
"documentId": "27730895",
"pageNumber": "1",
"tabLabel": "cPackage",
"value": ""
},
{
"documentId": "27730895",
"pageNumber": "1",
"tabLabel": "sDate",
"value": ""
},
{
"documentId": "27730895",
"pageNumber": "1",
"tabLabel": "pAddress",
"value": ""
},
{
"documentId": "27730895",
"pageNumber": "2",
"tabLabel": "rAmount",
"value": ""
}
]
}
}
]
}];
我创建它并使用表单中提交的用户数据附加它。这是我如何附加对象。
var contract_data = {
cEmail:req.body.cEmail,
FullName:req.body.cName,
pAddress:req.body.pAddress,
cAmount:req.body.cAmount,
rAmount:req.body.rAmount,
dCompleted:req.body.dCompleted,
cDate:req.body.cDate,
cPackage:req.body.cPackage
};
/data[0].templateRoles[1] = contract_data.cEmail;
data[0].templateRoles[2] = contract_data.FullName;
data[0].templateRoles[0].tabs.textTabs[0].value = contract_data.cPackage;
data[0].templateRoles[0].tabs.textTabs[1].value = contract_data.dCompleted;
data[0].templateRoles[0].tabs.textTabs[2].value = contract_data.pAdress;
data[0].templateRoles[0].tabs.textTabs[3].value = contract_data.rAmount;
然后我通过JSON.stringify()发送该数据数组,如此
var body = JSON.stringify(data)
发送到帖子并提出请求
request({
headers:{"X-DocuSign-Authentication":creds},
url: "https://demo.docusign.net/restapi/v2/accounts/3465212/envelopes",
json: true, // <--Very important!!!
body: body,
method: "POST",
}, function (error, response, body){
console.log(response.body);
});
答案 0 :(得分:1)
请求正文的根目录不应该是数组。
按如下方式更改。删除了[]
var data =
{
"documents": [
{
//Removed document properties for brevity
}
],
"emailSubject": "Please Sign document",
"status": "sent",
"templateId": "4fda9b93-9c82-4f23-bd06-67f9b35f0183",
"templateRoles": [
{
//Removed template roles for brevity
}
]
};
然后按如下方式更新模板角色。使用data
代替data[0]
,因为它不再是数组。
var tRole = data.templateRoles[0];
tRole.email = contract_data.cEmail;
tRole.name = contract_data.FullName;
tRole.tabs.textTabs[0].value = contract_data.cPackage;
tRole.tabs.textTabs[1].value = contract_data.dCompleted;
tRole.tabs.textTabs[2].value = contract_data.pAdress;
tRole.tabs.textTabs[3].value = contract_data.rAmount;