Как я могу отправить поля в теле json в параметре c в node js API? - PullRequest
0 голосов
/ 11 апреля 2020

Остальные API, которые я использую с express js ниже, отправляют запрос электронной подписи c на определенную почту пользователя. Он делает этот запрос со своим адресом электронной почты в теле json. Адреса электронной почты могут быть разными. Как я могу отправить поля email_address и name в теле opts параметрическим c способом в API? Основная проблема у меня заключается в том, как я могу параметрически отправить параметры email_address и name в json body. Я не хочу отправлять в жестком коде.

Express JS API:

const express = require('express');
const router = express.Router();
const hellosign = require('hellosign-sdk')({ key: 'key123' });
const fs = require('fs');

hellosign.account.update({
    callback_url: process.env.HOST_URL+'api/callback'
}).then((res) => {
    // handle response
    console.log("--- sign res ---");
    console.log(res);
}).catch((err) => {
    // handle error
    console.log(err)
});


router.post('/sign',(req,res)=>{

    res.send('Sign request is called');

    const opts = {
        test_mode: 1,
        title: 'PDF Sign via Node Server',
        subject: 'PDF Sign via Node Server',
        message: 'Please sign this pdf.',
        signers: [
            {
                email_address: 'example@gmail.com',
                name: 'Mr Okoreni'
            }
        ],
        files: ['nda.pdf']
    };

    hellosign.signatureRequest.send(opts).then((res) => {
        // handle response
        console.log(res);
    }).catch((err) => {
        // handle error
        console.log(err)
    });
});

router.post('/callback',(req,res)=>{

    res.send('Hello API Event Received');
    console.log('callback request is called');
    try{
        res.send(res.data);
        console.log(res.data);
        console.log(res.body);
    }catch (e) {
        console.log(e)
    }

});

router.get('/download/',(req,res)=>{

    res.send('download request is called');
    try {
        console.log('download request is called');

        var signatureRequestId = 'sign123';
        hellosign.signatureRequest.download(signatureRequestId, { file_type: 'zip' }, (err, res) => {
            const file = fs.createWriteStream("./downloads/"+signatureRequestId+"files.zip");

            res.pipe(file);

            file.on('finish', () => {
                file.close();
                console.log("finish");
            });
        });

    }catch (e) {
        console.log(e);
    }
});


router.get('/', (req, res) => {
    res.send("Server is listening...")
});

module.exports = router;

1 Ответ

0 голосов
/ 11 апреля 2020

Полностью зависит, откуда вы получаете параметризованные поля от

От запроса ..

const opts = {
    test_mode: 1,
    title: 'PDF Sign via Node Server',
    subject: 'PDF Sign via Node Server',
    message: 'Please sign this pdf.',
    signers: [
        {
            email_address: req.email_address,
            name: req.name,
        },
    ],
    files: ['nda.pdf'],
}

...