如果您想了解PostmanChrome:form-data、x-www-form-urlencoded和raw有什么区别和postman中form-data和raw区别的知识,那么本篇文章将是您的不二
如果您想了解Postman Chrome:form-data、x-www-form-urlencoded 和 raw 有什么区别和postman中form-data和raw区别的知识,那么本篇文章将是您的不二之选。我们将深入剖析Postman Chrome:form-data、x-www-form-urlencoded 和 raw 有什么区别的各个方面,并为您解答postman中form-data和raw区别的疑在这篇文章中,我们将为您介绍Postman Chrome:form-data、x-www-form-urlencoded 和 raw 有什么区别的相关知识,同时也会详细的解释postman中form-data和raw区别的运用方法,并给出实际的案例分析,希望能帮助到您!
本文目录一览:- Postman Chrome:form-data、x-www-form-urlencoded 和 raw 有什么区别(postman中form-data和raw区别)
- .net core运用application/x-www-form-urlencoded发起post请求
- angular – 使用x-www-form-urlencoded的HttpClient POST请求
- application / x-www-form-urlencoded或multipart / form-data?
- application/x-www-form-urlencode/multipart/form-data
Postman Chrome:form-data、x-www-form-urlencoded 和 raw 有什么区别(postman中form-data和raw区别)
我正在使用 Postman Chrome 扩展程序来测试 Web 服务。
有三个选项可用于数据输入。
我猜raw
是为了发送 JSON。
其他两个有什么区别,form-data
和x-www-form-urlencoded
?
答案1
小编典典这些是 W3C 定义的不同表单内容类型。如果你想发送简单的文本/ASCII 数据,那么 x-www-form-urlencoded
就可以了。这是默认设置。
但是,如果您必须发送非 ASCII 文本或大型二进制数据,则 表单数据 就是为此。
如果您想发送纯文本或 JSON 或任何其他类型的字符串,您可以使用 Raw 。 顾名思义,Postman
按原样发送您的原始字符串数据,无需修改。您可以使用下拉菜单中的内容类型标头来设置您要发送的数据类型。
*当您想将非文本数据附加到请求时,可以使用 *二进制,例如视频/音频文件、图像或任何其他二进制数据文件。
请参阅此链接以进一步阅读: HTML
文档中的表单
.net core运用application/x-www-form-urlencoded发起post请求
通常情况下都是使用 application/json发起post请求,但是有的.net 接口只接收 application/x-www-form-urlencoded
例如:
{
name:"张三",
results:[
{score:88,subject:"语文"}
]
}
需改为 name=张三&results[0][score]=88&results[0][subject]=语文方式
对常规的post请求进行处理,把json转为urlencoded
/// <summary>
/// 发起POST同步请求
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers">填充消息头</param>
/// <returns></returns>
public static string HttpPost(string url, string postData = null, Dictionary<string, string> headers = null, string contentType = "application/json", int timeOut = 30)
{
postData = postData ?? "";
if (contentType == "application/x-www-form-urlencoded")
{
postData = JsonUrlEncode(postData);
}
using (HttpClient client = new HttpClient())
{
if (headers != null)
{
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
{
if (contentType != null)
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
}
以下为json转urlencoded的方法和地柜
/// <summary>
/// json转urlencode
/// </summary>
/// <returns></returns>
public static string JsonUrlEncode(string json)
{
Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
StringBuilder builder = new StringBuilder();
foreach (KeyValuePair<string, object> item in dic)
{
builder.Append(GetFormDataContent(item, ""));
}
return builder.ToString().TrimEnd(''&'');
}
/// <summary>
/// 递归转formdata
/// </summary>
/// <param name="item"></param>
/// <param name="preStr"></param>
/// <returns></returns>
private static string GetFormDataContent(KeyValuePair<string, object> item, string preStr)
{
StringBuilder builder = new StringBuilder();
if (string.IsNullOrEmpty(item.Value?.ToString()))
{
builder.AppendFormat("{0}={1}", string.IsNullOrEmpty(preStr) ? item.Key : (preStr + "[" + item.Key + "]"), System.Web.HttpUtility.UrlEncode((item.Value == null ? "" : item.Value.ToString()).ToString()));
builder.Append("&");
}
else
{
//如果是数组
if (item.Value.GetType().Name.Equals("JArray"))
{
var children = JsonConvert.DeserializeObject<List<object>>(item.Value.ToString());
for (int j = 0; j < children.Count; j++)
{
Dictionary<string, object> childrendic = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(children[j]));
foreach (var row in childrendic)
{
builder.Append(GetFormDataContent(row, string.IsNullOrEmpty(preStr) ? (item.Key + "[" + j + "]") : (preStr + "[" + item.Key + "][" + j + "]")));
}
}
}
//如果是对象
else if (item.Value.GetType().Name.Equals("JObject"))
{
Dictionary<string, object> children = JsonConvert.DeserializeObject<Dictionary<string, object>>(item.Value.ToString());
foreach (var row in children)
{
builder.Append(GetFormDataContent(row, string.IsNullOrEmpty(preStr) ? item.Key : (preStr + "[" + item.Key + "]")));
}
}
//字符串、数字等
else
{
builder.AppendFormat("{0}={1}", string.IsNullOrEmpty(preStr) ? item.Key : (preStr + "[" + item.Key + "]"), System.Web.HttpUtility.UrlEncode((item.Value == null ? "" : item.Value.ToString()).ToString()));
builder.Append("&");
}
}
return builder.ToString();
}
原文出处:https://www.cnblogs.com/Chavezcn/p/11692825.html
angular – 使用x-www-form-urlencoded的HttpClient POST请求
login(username,password): Observable<any> { return this.http.post('/login',{ username: username,password: password },{ headers: new HttpHeaders() .set('Content-Type','x-www-form-urlencoded') } );
不幸的是我的API说我发送了空的用户名和密码。
所以我决定向我的登录端点发一个邮递员请求,看看问题来自哪里,邮递员请求确实返回了用户名和密码。
为什么当我从邮递员发帖时我的API返回我的用户名和密码,当我从我的Angular应用程序发布时,我的API返回空值?有什么我想念的吗?
下面的代码段应该可行。
login(username,password): Observable<any> { const body = new HttpParams() .set('username',username) .set('password',password); return this.http.post('/login',body.toString(),{ headers: new HttpHeaders() .set('Content-Type','application/x-www-form-urlencoded') } ); }
application / x-www-form-urlencoded或multipart / form-data?
问题:
In HTTP there are two ways to POST data: application/x-www-form-urlencoded
and multipart/form-data
. 在HTTP中有两种POST数据的方式: application/x-www-form-urlencoded
和multipart/form-data
。 I understand that most browsers are only able to upload files if multipart/form-data
is used. 据我所知,如果使用multipart/form-data
,大多数浏览器只能上传文件。 Is there any additional guidance when to use one of the encoding types in an API context (no browser involved)? 在API上下文中使用其中一种编码类型时是否有任何其他指导(不涉及浏览器)? This might eg be based on: 这可能基于:
- data size 数据大小
- existence of non-ASCII characters 存在非ASCII字符
- existence on (unencoded) binary data 存在于(未编码的)二进制数据上
- the need to transfer additional data (like filename) 需要传输额外的数据(如文件名)
I basically found no formal guidance on the web regarding the use of the different content-types so far. 到目前为止,我基本上没有在网上找到有关使用不同内容类型的正式指导。
解决方案:
参考一: https://stackoom.com/question/Goef/application-x-www-form-urlencoded或multipart-form-data参考二: https://oldbug.net/q/Goef/application-x-www-form-urlencoded-or-multipart-form-data
application/x-www-form-urlencode/multipart/form-data
首先我们先认识下今天的application/x-www-form-urlencode/multipart/form-data属性所在的位置
1、form所属
在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。 例如: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,这个一般文件上传时用。 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
2、不同的编码方式
form的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application/x-www-form-urlencoded。
当数据传递的方式是get的时候,浏览器使用application/x-www-form-urlencode的编码方式,把form数据转换为一个字符串,如(name=zhangsan&age=128),然后把这个字符添加到url后面,用?连接 组成新的url并加载
当数据传递的方式是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)。(ps:第二种方式的解释本人还有些模糊,有更为精确答案的猿,希望分享下)
我们今天的关于Postman Chrome:form-data、x-www-form-urlencoded 和 raw 有什么区别和postman中form-data和raw区别的分享已经告一段落,感谢您的关注,如果您想了解更多关于.net core运用application/x-www-form-urlencoded发起post请求、angular – 使用x-www-form-urlencoded的HttpClient POST请求、application / x-www-form-urlencoded或multipart / form-data?、application/x-www-form-urlencode/multipart/form-data的相关信息,请在本站查询。
本文标签: