How format special character in formData by nodeJS ?
I have a problem with encoding on special characters, let me show my code :
I have a node which post request directly on website, so i use FormData object to build form data.
The request did by a browser (got the CURL) : In all example, it's a sample of my data where i have the problem.
$'-----------------------------1755811723502268269588155\r\n
Content-Disposition: form-data; name="pageId"\r\n\r\n212403390500\r\n-----------------------------1755811723502268269588155\r\n
Content-Disposition: form-data; name="CL_TEXT_CLOT"\r\n\r\nLa t\xe2che \xe9tait bloqu\xe9\r\n-----------------------------1755811723502268269588155\r\n
the code is :
const form = new FormData();
form.append('pageId', pageId),
form.append('pageId', pageId),
form.append('CL_TEXT_CLOT',el.CL_TEXT_CLOT)
...
let configPost = {
method: 'POST',
headers : {'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,/*;q=0.8",
'Content-Type': 'multipart/form-data; boundary='+form._boundary,
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36'},
url: 'http://localhost:7000/test',
data : form,
validateStatus: function (status) {
return status >= 200 && status < 300 || status == 302; // default
}
When i sent :
'----------------------------397600554882409697883429\r\nContent-Disposition: form-data; name="CL_TEXT_CLOT"\r\ntype: text/plain; charset=UTF-8\r\n\r\n',
'La tâche était bloqué.',
If i use escapeUnicode function :
let escapeUnicode = function (str) {
return str.replace(/[^\0-~]/g, function(ch) {
let tmp = "\\u" + ("000" + ch.charCodeAt().toString(16)).slice(-4);
return tmp.replace('\\u00','\\x')
});
}
in the formData, same code but with this line :
form.append('CL_TEXT_CLOT', escapeUnicode(el.CL_TEXT_CLOT))
and the result is :
'----------------------------882802935214785196311782\r\nContent-Disposition: form-data; name="CL_TEXT_CLOT"\r\ntype: text/plain; charset=UTF-8\r\n\r\n',
'La t\\xe2che \\xe9tait bloqu\\xe9.',
formData encapsulate the \xe2 by \ so result is \xe2....
and the final code is this (with or without the escapeUnicode) :
const form = new FormData();
form.append('CL_TEXT_CLOT', escapeUnicode(el.CL_TEXT_CLOT),{ header: { type: 'text/plain; charset=UTF-8' } })
Do you have an idea ? Maybe i am in the wrong way....
EDIT: I have the same program in Python, and i had this in data :
<MultipartEncoder: [('CL_TEXT_CLOT', 'The ticket is clos\xe9d.'), ('actionClose', 'Clore'),]>
So how should i do to have special character escaped in the formData in nodeJS ???