Datatype_mismatch in tickets attachment

Hello,

I’m writing a custom enquiries service using the tickets API. I’m having issues with attachments. I’ve searched in the forum and others have the same issue, but without clear solution.
This is the error I get:

description: "Validation failed"
errors: Array [ {…} ]
0: Object { field: "attachments", message: "It should contain elements of type valid file format only", code: "datatype_mismatch" }

Yet I’m sending an array of objects as written in the documentation.

At the moment my server is reading an incoming form and parsing it with Formidable to deal with multi-part form data. The attachments are read with fs and attached to the request for the Freshdesk API.

This is the code:

app.post('/enquiry', (req, res) => {
  const form = formidable({
    maxTotalFileSize: 20 * 1024 * 1024,
  })
  
  form.parse(req, async (err, fields, files) => {
    if (err) {
      res.json(err)
      return
    }

    const formData = new FormData()

    for (const [key, value] of Object.entries(fields)) {
      formData.append(key, value[0])
    }

    if (files.attachments) {
      for (const a of files.attachments) {
        formData.append('attachments[]', fs.readFileSync(a.filepath))
      }
    }

    await fetch(`${process.env.FRESHDESK_ORIGIN_URL}/tickets`, {
      method: 'POST',
      body: formData,
      headers: {
        ...headers,
      },
    })
      .then((r) => r.json())
      .then((r) => res.json(r))
  })
})

thanks a lot for your help :slight_smile:

Hi @km0

From the provided code snippet, it seems that you’re correctly parsing the incoming form data using Formidable and appending the attachments to the FormData object. However, the issue might lie in how the attachments are being read and attached to the request.

One potential area to investigate further is the format of the attachments being read using fs.readFileSync(a.filepath). It’s important to ensure that the attachments are read and attached to the FormData object in the correct format expected by the Freshdesk API.

Have you tried the same attachment through Postman?

Hey Ajith thanks for your answer

I did manage to get the snippet working by using the library unirest as showed in this Freshdesk example. To be honest, I’m still a bit puzzled about why with the native fetch and the same implementation I get the error, but for now this solution is ok.

Here my updated code:

app.post('/tickets', (req, res) => {
  const form = formidable({
    maxTotalFileSize: 20 * 1024 * 1024,
    keepExtensions: true,
  })

  form.parse(req, async (err, fields, files) => {
    if (err) {
      res.json(err)
      return
    }

    const request = unirest
      .post(`${process.env.FRESHDESK_ORIGIN_URL}/tickets`)
      .headers(headers)
      .field(fields)

    if (files.attachments) {
      for (const a of files.attachments) {
        request.attach('attachments[]', fs.createReadStream(a.filepath))
      }
    }

    await request.end(function (response) {
      res.json(response)
    })
  })
})

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.