微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何找出 Unirest 给出 422 响应的原因?

如何解决如何找出 Unirest 给出 422 响应的原因?

几个月前,我最近在 IT 领域找到了一份工作,但我们的一位客户要求进行一些自定义配置。我从软件开发人员训练营毕业,所以我想我会自愿做这个配置(以便从训练营中得到一些利用)。我没有专门为此获得报酬,但如果我无法弄清楚它会让我看起来很糟糕。只是说明以防万一有人认为我想为他们做我的工作而获得报酬。

定制与名为 DocuWare 的软件有关。此代码登录到 DocuWare 以查看文档是否已使用特定参数存储(因此文档不能存储两次)。如果您愿意,可以在 https://github.com/DocuWare/Validation-NodeJS-samples/tree/a30a9940dad68a7e3b81b46018cdef19a6f495a4

查看 DocuWare 提供的完整示例代码

我已经成功地通过代码找出每个函数的作用和顺序。我还能够通过 POST 发送一些 JSON 来手动测试函数是否正常工作

{
"UserName": "admin","OrganizationName": "Peters Engineering","FileCabinetGuid": "a82a0ea6-16bd-4f18-a980-fda842922757","DialogGuid": "e61a85dc-beca-4fbd-a1fc-bff811854a4d","DialogType": "Store","Values": [{
    "FieldName": "INVOICE_NUMBER","ItemElementName": "String","Item": 1234
},{
    "FieldName": "DOCUMENT_DATE","ItemElementName": "Date","Item": "2021-07-29T00:00:00Z"
},{
    "FieldName": "vendOR_NAME","Item": "Company"
},{
    "FieldName": "DOCUMENT_TYPE","ItemElementName": "Int","Item": "Invoice"
},{
    "FieldName": "NET_AMOUNT","ItemElementName": "Decimal","Item": 1.20
}]
}

我坚持使用的是 Unirest(我以前从未使用过)。使用 console.log,我能够确定错误来自以下代码块:

exports.isDuplicateInvoice = function (fileCabinetGUID,invoiceNo,invoiceDate,supplierID) {
return new Promise((resolve,reject) => {   
    var CookieJar = unirest.jar(true);

    //logon to DW PLATFORM and retrieve cookie;
    unirest.post(DWparameters.DWPlatformUrl + '/Account/logon')
    .headers({'Accept': 'application/json','Content-Type': 'application/x-www-form-urlencoded'})
    .jar(CookieJar)
    .send({ 'UserName': DWparameters.DWPlatformUser,'Password': DWparameters.DWPlatfromPassword,'Organization': DWparameters.DWPlatformOrganization,'RememberMe': false,'RedirectToMyselfInCaSEOfError': true })
    .end(function (response) {
        // console.log(response,"RESPONSE")
        unirest.post(DWparameters.DWPlatformUrl + '/FileCabinets/'+ fileCabinetGUID +'/Query/DialogExpression?dialogId='+ DWparameters.DWSearchDialogGUIDForInvoiceSearch +'&format=table')
        .headers({'Accept': 'application/json','Content-Type': 'application/json'})
        .jar(CookieJar)
        .send({ 'Condition':[
            { 'dbname':DWparameters.fieldNameDOCNUMBER,'Value': [invoiceNo] },{ 'dbname':DWparameters.fieldNameDOCDATE,'Value': [invoiceDate,null] },{ 'dbname':DWparameters.fieldNamesupplier,'Value': [supplierID,],'SortOrder':[],'ForceRefresh':true,'FlagConditions':{'IncludeCheckedOut':false},'Operation':'And','AdditionalResultFields':[],'Start':0,'Count':1})
        .end(function (response) {
            if (response.error) {
                //Console shows below error
                console.log("Error coming from here")
                return reject(new Error(response.error.message));
            }


            try {
                var resultCount = response.body.Count.Value;
                return resolve(resultCount == 0);               
            } catch (error) {
                return reject(new Error("Unable to retrieve similar invoices. Error:" + error));
            }
        });
    });
})
}

我确保输入了正确的 FileCabinetGuid、DialogGuid 和 DWSearchDialogGUIDForInvoiceSearch。但是当我将某些内容发布到正确的 URL 时,我收到了 { "Status": "Fail","Reason": "got 422 response" } Google 曾表示 422 错误是:

The HyperText Transfer Protocol (HTTP) 422 Unprocessable Entity response status code indicates that the server understands the content type of the request entity,and the Syntax of the request entity is correct,but it was unable to process the contained instructions.

不过,我不知道这到底是什么意思。我怎么知道哪个部分出了问题?我如何手动测试它? (我并没有真正学习像 Jasmine 之类的测试)。如果有人能帮忙,你会很棒!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。