关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related

本文深入探讨了HTTP请求头中两种常见的Content-Type格式:application/x-www-form-urlencoded和multipart/related,解释了它们的区别及在实际应用中的影响,通过案例分析帮助开发者理解如何正确设置和处理不同类型的表单数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原帖地址:

关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related - 无法更新的~ - 博客园
http://www.cnblogs.com/taoys/archive/2010/12/30/1922186.html



最近项目中用到的一个是用一个页面接收c程序post过来的一断字符串..总接收不到值...

我用C#写一个测试可以正常接收到值.

最后抓包比较

 

  
  
区别只是
Content
- Type:application / x - www - form - urlencoded

Content
- Type:multipart / related

查资料得:

 application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
 补充
form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。 当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2...),然后把这个字串append到url后面,用?分割,加载这个新的url。 当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。


### Payload vs Form Data in HTTP Requests In the context of HTTP requests, both **payload** and **form data** refer to different ways of sending information from a client to a server. Understanding how each works helps developers choose the appropriate method based on their needs. #### Definition and Usage A payload generally refers to any data sent within an HTTP request body that is intended for processing by the recipient service or application. This term encompasses various formats including JSON, XML, plain text, binary files, etc., depending upon what type of content-type header has been specified[^1]. Form data specifically pertains to key-value pairs transmitted via POST methods using `application/x-www-form-urlencoded` or `multipart/form-data`. The former encodes characters as URL parameters while multipart form data allows uploading multiple pieces of data such as file attachments alongside textual inputs without being constrained by size limits imposed by URLs[^2]. #### Differences Between Payload and Form Data The primary distinction lies in encoding mechanisms used during transmission: - With payloads, applications have greater flexibility regarding format choice since they aren't restricted solely to simple name-value collections. - Conversely, traditional web forms typically utilize either urlencoded strings (suitable only for small amounts of non-binary info) or multi-part entities designed primarily around submitting documents along with metadata describing them like filenames & MIME types[^3]. For example, consider this Python code snippet demonstrating how one might construct these two kinds of submissions programmatically: ```python import requests # Example of sending JSON payload json_payload = {"key": "value"} response_json = requests.post('https://example.com/api', json=json_payload) # Example of posting form-encoded data form_data = {'field': 'data'} files = {'file_field': open('document.pdf', 'rb')} response_form = requests.post('https://example.com/upload', data=form_data, files=files) ``` --related questions-- 1. What are some common use cases where you would prefer using a JSON payload over form data? 2. How does setting Content-Type affect handling of incoming requests at the server side? 3. Can you explain under which circumstances should multipart/form-data be utilized instead of application/x-www-form-urlencoded? 4. Are there performance implications associated with choosing between these submission styles? 5. In RESTful API design principles, do recommendations exist concerning selection among available media types?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值