Axios経由でGoogle APIにPDFを送信する際に問題が発生しました。サーバ上のAxios経由でGoogle APIを送信する(NodeJs)
コードは、私が試した:
const express = require('express');
const axios = require('axios');
const router = express.Router();
const PDFDocument = require('pdfkit');
const ticketProperties = {
'version': '1.0',
'print': {}
};
router.post('/print/sale', async (req, res) => {
// Generate test PDF
let doc = new PDFDocument;
doc.pipe(fs.createWriteStream('output.pdf'));
doc.fontSize(8)
.text('Some text example for pdf', 1, 1);
doc.end();
// Once the pdf is generated, read it and send it via axios
axios.post(
'https://www.google.com/cloudprint/submit',
{
printerid : 'PRINTER_ID_REMOVED_INTENTIONALLY',
title: 'pdf print',
ticket: ticketProperties,
content : doc,
contentType: 'application/pdf'
},
{
headers: {
'Authorization': 'Bearer ACCESS_TOKEN_REMOVED_INTENTIONALLY',
'Content-Type': 'application/pdf'
}
}
)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
});
私はconsole.log(error)
に入ったエラー私はそのAPIを呼び出す:
TypeError: Converting circular structure to JSON at JSON.stringify() at transformRequest (G:\projects\pos-web\pos-backend\node_modules\axios\lib\defaults.js:51:19) at transform (G:\projects\pos-web\pos-backend\node_modules\axios\lib\core\transformData.js:16:12) at Object.forEach (G:\projects\pos-web\pos-backend\node_modules\axios\lib\utils.js:224:12) at transformData (G:\projects\pos-web\pos-backend\node_modules\axios\lib\core\transformData.js:15:9) at dispatchRequest (G:\projects\pos-web\pos-backend\node_modules\axios\lib\core\dispatchRequest.js:37:17) at
私は、ファイルを書いているので、そのエラーがあると思いますポストの体内のdoc
。以前に処理する必要がありますか?
注:
- PDFは私のプロジェクトの内部で正常に生成されます。
- 私の最初のテストはpdfの代わりにテキストを送信していてOKでした。
- 私は可能な解決策をたくさん試しましたが、私にさまざまなエラーを与えました。おそらく、サーバーからpdfを送信する方法が間違っていると思います。 お願いAxiosではなく別のライブラリがある場合や、サーバーからpdfを送信する方法の流れを知っている場合は、躊躇しないでください。
詳細情報:エンドポイントhttps://www.google.com/cloudprint/submit
について
- :Link