How to use the formdata in the client request method? | The place for Zendesk users to come together and share
Skip to main content
fahev
September 4, 2023
Question

How to use the formdata in the client request method?

  • September 4, 2023
  • 13 replies
  • 70 views

I making an API calls inside the SDK application with the Axios POST method and Formdata, you can in the below code.

const axios = require('axios');
const FormData = require('form-data');
let data = new FormData();
data.append('username', 'balu');
data.append('key', jhjjjjhjjjhh');
data.append('email', 'anish.k@spritle.com');

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://opencart.ajithr.com/index.php?route=zendesk/customer',
  headers: { 
    'Content-Type': 'application/json', 
    'Cookie': 'OCSESSID=0c4898f4cd2a443a21c3fb3324; currency=USD', 
    ...data.getHeaders()
  },
  data : data
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});

How can I change this Axios method into a client method? 

I documentation also can't able to find it.

Thanks in advance.

13 replies

rajesh14
November 27, 2024

@erica26 
i add ‘application/binary’ as contentType but still not work 

 

Erica
Employee
December 3, 2024
Have you tried uploading the media first via the Upload API and then sending it with the token? Even though you're sending this request to an external server, if you're passing it through the proxy first you might want to try uploading it to your instance then making the request. 
Labor
May 28, 2025

I did something very similar to your use case. Give this a try:

(function waitForZendeskForm() {
 const formCheck = setInterval(() => {
   const form = document.querySelector('form[action*="/requests"]');
   if (form) {
     clearInterval(formCheck);

     form.addEventListener("submit", function (e) {
       e.preventDefault(); // stop default submission

       const values = new FormData(form);
       const newForm = document.createElement("form");
       newForm.method = "POST";
       newForm.action = form.action;
       newForm.style.display = "none";

       // Populate cloned form with actual values
       for (const [name, value] of values.entries()) {
         const input = document.createElement("input");
         input.type = "hidden";
         input.name = name;
         input.value = value;
         newForm.appendChild(input);
       }

       document.body.appendChild(newForm);
 

       // Listen for form submission to complete via fetch
       fetch(form.action, {
         method: 'POST',
         body: values,
         credentials: 'include'
       }).then(response => {
         if (response.ok) {
           window.location.href = 'https://xxxxxxxxxxxxxx.com';  // redirect to external site
         } else {
           console.error('Form submission failed:', response.status);
         }
       }).catch(err => {
         console.error('Form submission error:', err);
       });
     });
   }
 }, 500);
})();