shopify-app-template-node icon indicating copy to clipboard operation
shopify-app-template-node copied to clipboard

form-data use for image upload but req.body and req.file in no data available.

Open chiragDesaiPantherCodx opened this issue 3 years ago • 2 comments

image

Here my backend code for create product image

chiragDesaiPantherCodx avatar Aug 24 '22 13:08 chiragDesaiPantherCodx

It's unclear if this is an Express app or if it's using some other framework.

Assuming it's something like Express, unless you've used a body parser middleware, you'll likely need to call req.body.text() or req.body.json() to read in the body. It may still be in stream format.

mkevinosullivan avatar Sep 23 '22 16:09 mkevinosullivan

@chiragDesaiPantherCodx @mkevinosullivan It uses Express, also the template already includes the body parser middleware for JSON. You just need to include your routes AFTER the following line in the server.js/index.js file that your server is in and you should be able to make any request that uses the .body attributes work fine after that.

  // All endpoints after this point will have access to a request.body
  // attribute, as a result of the express.json() middleware
  app.use(express.json());

For files and uploads, you just need to add the following line:

app.use(express.urlencoded({ extended: true }))

and make sure you add your routes after that line for it to work properly.

mlagace avatar Nov 22 '22 17:11 mlagace