Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send an array of pdf files using form-data #553

Open
madhukiranvarma opened this issue Jul 29, 2023 · 5 comments
Open

How to send an array of pdf files using form-data #553

madhukiranvarma opened this issue Jul 29, 2023 · 5 comments

Comments

@madhukiranvarma
Copy link

I'm trying to send an array of pdf files to backend using form-data lib

I was trying to append pdf files like this:

const formData = new FormData();
for (const file of files) {
       formData.append('file[]', fs.createReadStream(file.path), {
               filename: `${file.type}_${file.month}.pdf`,
        });
}

and this is the axios request

Axios.request({
        method: 'POST',
        url,
        headers: {
            'Content-Type': data.formData.getHeaders()['content-type'],
        },
        data: data.formData,
    });

but I'm getting this error, everytime I make an axios request

node_modules/express/lib/response.js:1150
    : JSON.stringify(value);
           ^
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'curlObject' -> object with constructor 'CurlHelper'
    --- property 'request' closes the circle
    at JSON.stringify (<anonymous>)

can anyone please tell me what's the issue here?

@jimmywarting
Copy link

jimmywarting commented Sep 6, 2023

I would say that you don't need this pkg anymore.

const formData = new FormData()

for (const file of files) {
  const blob = await fs.openAsBlob(file.path)
  formData.append('file[]', blob, `${file.type}_${file.month}.pdf`)
}

fetch(url, { method: 'POST', body: formData })

fetch, blob and formdata is built in globally now.

@glhrmv
Copy link

glhrmv commented Sep 6, 2023

@jimmywarting I am getting Cannot find name 'FormData'. compilation error from TypeScript using Node 18.16.1, is there something I need to configure to let TypeScript aware of this global?

@jimmywarting
Copy link

jimmywarting commented Sep 6, 2023

if you are going to use fs.openAsBlob(path[, options]) then you will need v19.8+
maybe try to install latest @types/node

@glhrmv
Copy link

glhrmv commented Sep 6, 2023

@jimmywarting Should have clarified that I am not using openAsBlob, the issue is strictly related to the first line const formData = new FormData(). And I have the latest @types/node. :(

@jimmywarting
Copy link

jimmywarting commented Sep 6, 2023

I am not using openAsBlob

Built in FormData is not going to accept a readableStream... if that is what you are using. saw you where using fs.createReadStream(file.path)

And I have the latest @types/node. :(

sry, can't help you there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants