请求发布使用Node JS Express获取400状态代码

时间:2016-05-16 05:50:12

标签: javascript node.js request

我有以下代码将文档发布到特定位置。一切都是正确的但不知何故我得到了400 Bad Request。有人可以建议为什么会这样。

以下是我的代码。

function storeDocuments(req, res, tokenKey, data) {
    var uniqueCode = getClientCode(req, res);
    var filePath = path.normalize(data.pdfURL);
    var boundaryKey = Math.floor(Math.random() * 1E16);
    var options = { method: 'POST',
        url: 'https://api.fundpress.io/docloader/adddocument',
        headers: {
            'content-type': 'multipart/form-data; charset=utf-8; boundary=---' + boundaryKey,
            'cache-control': 'no-cache',
            'X-KSYS-TOKEN': tokenKey
        },
        formData: {
            file: { value: fs.createReadStream(filePath),
                options: {
                    filename: "performance_report.pdf",
                    contentType: 'application/pdf'
                }
            },
            cultureCode: res.locals.locale,
            title: uniqueCode,
            meta: '{ "doctype":{ "value":[' + uniqueCode + '] } }',
            clientCode: uniqueCode
        }
    };
    logger.info('Making request to: ', options.url);
    request(options, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            logger.info('Request Completed: ', options.url, 'BODY_LENGTH: ', (body && body.length));
        } else {
            if (response && response.statusCode === 400) {
                logger.warn('Upload Request failed with 400 (Bad Request), URL:', options.url);
                logger.warn('Upload Request failed with 400 (Bad Request), BODY:', body);
                logger.warn('Upload Request failed with 400 (Bad Request), ERROR:', error);
            } else {
                logger.error('Upload Request error, URL:', options.url);
                logger.error('Upload Request error, STATUS_CODE:', response && response.statusCode);
                logger.error('Upload Request error, BODY:', body);
                logger.error('Upload Request error, ERROR:', error);
            }
        }
    });
    res.end();
}

0 个答案:

没有答案
相关问题