Hi Team,
With post i am trying to send formData to the server end to push it to the elasticsearch. but the formData is always empty in the server end. I have data in formData from the client side. can someone help me if any corrections in the below code.
Below is the code for client side
const handleSubmit = async (event) => {
event.preventDefault();
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData), // Convert formData to JSON string
};
try {
// Make a POST request to the backend server API with the form data
http
.post('/api/workitem_register/post_data', requestOptions)
.then((response) => {
// Handle the response
})
.catch((error) => {
// Handle errors
});
notifications.toasts.addSuccess(
i18n.translate('workitemRegister.formSubmitted', {
defaultMessage: 'Form submitted successfully',
})
);
} catch (error) {
console.error('Error submitting form:', error);
notifications.toasts.addError(
i18n.translate('workitemRegister.formSubmitError', {
defaultMessage: 'Error submitting form',
})
);
}
};
Below is server side code
router.post(
{
path: '/api/workitem_register/post_data',
validate: false, // No request validation needed
},
async (context, request, response) => {
try {
// Extract form data from the request body
const formData = request.body;
// Store the form data in Elasticsearch using esClient
await esClient.index({
index: 'my_index', // Specify the index where you want to store the document
body: formData,
});
// Send a success response back to the client
return response.ok({
body: { message: 'Form data submitted successfully!' },
});
} catch (error) {
// Handle errors
console.error('Error handling form submission:', error);
return response.customError({ statusCode: 500, body: 'Internal server error.' });
}
}
);
@carly.richmond any clue or help on this issue