Post files with the request method

I’m currently developing a custom Freshdesk app that opens a modal (client.interface.trigger('showModal', ...). The modal contains a form that posts data to our company issue tracker.

This is all working perfectly with the recommended approach: client.request.post....

The only thing that I can’t figure out is how to post a file to an endpoint through the Freshdesk request method. Is this supported or should I look for another solution?

@joren
Good Day! Welcome to the community!

you can, if you are using multipart form data

eg:

    let url = `https://${domain}.freshdesk.com/api`;
    let headers = { "Authorization": "Token token=<%= iparam.apiKey %>", "Content-Type": "multipart/form-data" }; 
    let options = { headers: headers, body: body };
    return Client.request.post(url, options)

in request API

Hope it helps :slight_smile:

Thanks

Thanks for the quick reply!

The API (Redmine) that I’m sending a file to expects Content-Type: application/octet-stream.

From the API docs:

The request body should be the content of the file you want to attach and the Content-Type header must be set to application/octet-stream (otherwise you’ll get a 406 Not Acceptable response). If the upload succeeds, you get a 201 response that contains a token for your uploaded file.

I guess if multipart/form-data is the only option, I’ll have to setup a proxy to process the file?

you can try with application/octet-stream if it is not supported then try with multipart/form-data

No success so far.

I’ve tried using the multipart/form-data content type header with an instance of FormData in the body. That resulted in:

fresh_client.js:6 Uncaught (in promise) DOMException: Failed to execute ‘postMessage’ on ‘MessagePort’: FormData object could not be cloned.
at n.value (https://static.freshdev.io/fdk/2.0/assets/fresh_client.js:6:25992)
at https://static.freshdev.io/fdk/2.0/assets/fresh_client.js:6:25915

I also tried application/octet-stream with a png file in the body, that resulted in a 400: Error while substituting the templates.

Replacing the png file in the body with an empty string does not result in an error. Is it possible that the freshdesk client can not handle files in the body?

Hey @joren,

If requestAPI is not supported you can use other libs such as axios/fetch

Thanks