XMLHttpRequest multipart / form-data: Invalid border in multipart

I am sending data after XMLHttpRequest:

var xmlHttp=new XMLHttpRequest(); xmlHttp.open("POST", domain, true); xmlHttp.setRequestHeader("Content-type","multipart/form-data"); var formData = new FormData(); formData.append("data", data_json_string); xmlHttp.send(formData); 

In Python, I get an error if I try to get POST data (or FILES or something else):

 MultiPartParserError: Invalid boundary in multipart: None 

Could this work? Do I really need to create the body of the form as one line, where I go through the parameters and place the boundary line before and after each? And if so, what should it look like? How to get it from my POST in Python? Or there is an easier way. I look back and do not find much on this.

btw, I use "multipart / form-data" because my string data is really long and this is a faster way to send it. This worked for me when I create a form and submit it, targeting it with an iframe. But here I prefer xmlHttp.

+7
javascript python
source share
1 answer

Do not set the Content-Type header yourself. It will be set correctly when the .send() data, including the proper generated border, is missing from your manually created header.

The spec clearly states that .send(FormData) will use multipart / form-data encoding.

If the data is FormData p>

Let the body of the request object be the result of running the multipart / form-data encoding algorithm with data in the form of a form data set and with UTF-8 as explicit character encoding.

Let the mime type be the concatenation of "multipart / form-data;", the character SP + SPO U + 0020, "border =" and the border line multipart / form-data generated by the multipart / form-data encoding algorithm,

+11
source

All Articles