0

I am trying to POST to this endpoint using nodeJS's https.request() function but I'm getting a "missing file" response, however, it is able to read when I POST the clientId, so I'm thinking that "file" type inputs in multipart forms are possibly handled differently? Am I not POSTing in the correct manner?

This is the HTML form representation on the client's end that I'm POSTing too:

<html><body><form action='https://example.com/foo/bar/test' method='POST' enctype='multipart/form-data'>
        <input type="file" name="file">
        <input type="hidden" name="clientId" value="12345">
        <input type="submit" name="submit">
</form></body></html>

And this is my backend code for POSTing to it:

const data = JSON.stringify({
    file: pathToPdf,
    clientId: 6789
})

const options = {
    hostname: 'example.com',
    path: '/foo/bar/test',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    auth: 'user:pass'
};

`const req = https.request(options, (res) => {
    console.log('STATUS: ${res.statusCode}');
    console.log('HEADERS: ${JSON.stringify(res.headers)}');
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
        console.log('BODY: ${chunk}');
    });
    res.on('end', () => {
        console.log('No more data in response.');
    });
})
req.on('error', (e) => {
    console.error('problem with request: ${e.message}');
});

req.write(data);
req.end();

Is there a reason why "clientId" would work but not "file"?

Jakkie Chan
  • 35
  • 2
  • 8
  • Hey Jakkie, take a look at https://stackoverflow.com/questions/25344879/uploading-file-using-post-request-in-node-js With express.js you can use `req.file` for loading the uploaded file sent in a post request. – Luke Brown Dec 01 '19 at 01:21
  • 1
    Well at first glance, you're not sending a file. Your data object contains a file property whose value is "pathToPdf", which I'm guessing is a string. Also you're not sending "multipart/form-data" you're sending "application/json". – gforce301 Dec 01 '19 at 01:39

0 Answers0