multipart/form-data 的边界是什么? [英] What is the boundary in multipart/form-data?

查看:56
本文介绍了multipart/form-data 的边界是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个关于 multipart/form-data 的问题.在 HTTP 标头中,我发现 Content-Type: multipart/form-data;边界=???.

I want to ask a question about the multipart/form-data. In the HTTP header, I find that the Content-Type: multipart/form-data; boundary=???.

??? 可以由用户自由定义吗?还是从 HTML 生成的?我可以定义 吗???= abcdefg?

Is the ??? free to be defined by the user? Or is it generated from the HTML? Is it possible for me to define the ??? = abcdefg?

推荐答案

??? 可以由用户自由定义吗?

Is the ??? free to be defined by the user?

是的.

还是由 HTML 提供?

or is it supplied by the HTML?

没有.HTML nothing 与此有关.阅读下文.

No. HTML has nothing to do with that. Read below.

我可以将 ??? 定义为 abcdefg 吗?

Is it possible for me to define the ??? as abcdefg?

是的.

如果您想将以下数据发送到网络服务器:

If you want to send the following data to the web server:

name = John
age = 12

使用 application/x-www-form-urlencoded 会是这样的:

name=John&age=12

如您所见,服务器知道参数是用 & 符号 & 分隔的.如果参数值需要 & 则必须对其进行编码.

As you can see, the server knows that parameters are separated by an ampersand &. If & is required for a parameter value then it must be encoded.

那么当服务器使用multipart/form-data接收到一个HTTP请求时,如何知道一个参数值在哪里开始和结束呢?

So how does the server know where a parameter value starts and ends when it receives an HTTP request using multipart/form-data?

使用边界,类似于&.

例如:

--XXX
Content-Disposition: form-data; name="name"

John
--XXX
Content-Disposition: form-data; name="age"

12
--XXX--

在这种情况下,边界值为XXX.您在 Content-Type 标头中指定它,以便服务器知道如何拆分它接收到的数据.

In that case, the boundary value is XXX. You specify it in the Content-Type header so that the server knows how to split the data it receives.

所以你需要:

  • 使用不会出现在发送到服务器的 HTTP 数据中的值.

  • Use a value that won't appear in the HTTP data sent to the server.

保持一致并在请求消息中的所有位置使用相同的值.

Be consistent and use the same value everywhere in the request message.

这篇关于multipart/form-data 的边界是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆