如何从Postman到WebApi获取表单数据 [英] How to get form data from Postman To WebApi
问题描述
我想从邮递员那里接收表格数据:
I want to receive form data from Postman:
内容类型:application/json
这是WebApi方法:
Here is WebApi method:
[HttpPost]
[Route("api/test")]
public async Task TestMethod(HttpRequestMessage request)
{
var test = await request.Content.ReadAsStringAsync();
}
我得到的是:
------WebKitFormBoundarypqDvmeG89cBR9mK9
Content-Disposition: form-data; name="test"
esad
------WebKitFormBoundarypqDvmeG89cBR9mK9--
但是我不希望使用 WebKitFormBoundary
的数据,并且我限制只能使用formdata.还有其他办法吗?
But I don't want data with WebKitFormBoundary
and I've restriction to use formdata only. Is there any other way?
HTTP调用信息:
POST /api/test HTTP/1.1
Host: localhost:16854
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 1a3d6427-4956-707d-da0c-3a29a63c7563
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="test"
esad
------WebKitFormBoundary7MA4YWxkTrZu0gW--
卷曲呼叫信息:
curl -X POST \
http://localhost:16854/api/test \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-H 'postman-token: 02055873-e9a8-e9a6-019c-b407992b0e2f' \
-F test=esad
推荐答案
1)如果您必须发送 Content-Type:multipart/form-data
或仅发送 form-数据
1) If you have to send Content-Type: multipart/form-data
OR simply form-data
这是邮递员的第一个 tab
如果您只需要收集发布的 form-data
If you have to collect only one key/value pair of your posted form-data
[HttpPost]
[Route("api/test")]
public HttpResponseMessage TestMethod(HttpRequestMessage request)
{
var testValue = HttpContext.Current.Request.Form["test"];
return Request.CreateResponse(HttpStatusCode.OK, testValue);
}
如果您必须收集发布的 form-data
If you have to collect more than one key/value pair of your posted form-data
[HttpPost]
[Route("api/test")]
public HttpResponseMessage TestMethod(HttpRequestMessage request)
{
NameValueCollection collection = HttpContext.Current.Request.Form;
var items = collection.AllKeys.SelectMany(collection.GetValues, (k, v) => new { key = k, value = v });
//We just collect your multiple form data key/value pair in this dictinary
//The following code will be replaced by yours
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
foreach (var item in items)
{
keyValuePairs.Add(item.key, item.value);
}
return Request.CreateResponse(HttpStatusCode.OK, keyValuePairs);
}
2)如果您必须发送 Content-Type:application/x-www-form-urlencoded
2) If you have to send Content-Type: application/x-www-form-urlencoded
这是邮递员的第二个 tab
那么您的API将是
[HttpPost]
[Route("api/test")]
public async Task TestMethod(HttpRequestMessage request)
{
var test = await request.Content.ReadAsFormDataAsync();
}
然后,当您使用断点调试代码时,您将得到以下输出
Then you will get following output when you debug your code with breakpoint
3)如果您必须发送 Content-Type:application/json
3) If you have to send Content-Type: application/json
这是邮递员的第三个 tab
请参见下面的屏幕快照以了解该选项
See below screenshot for such option
您的api是
[HttpPost]
[Route("api/test")]
public async Task TestMethod(HttpRequestMessage request)
{
var jObject = await request.Content.ReadAsAsync<JObject>();
Item item = JsonConvert.DeserializeObject<Item>(jObject.ToString());
}
和您的模型来收集此发布的数据
And your model to collect this posted data
public class Item
{
public string test { get; set; }
}
您的输出将是
此选项的优点是,您可以发送复杂类型
作为发布的数据,例如
The advantage of this option you can send complex type
as posted data and like
您的api是
[HttpPost]
[Route("test")]
public async Task TestMethod(HttpRequestMessage request)
{
var jObject = await request.Content.ReadAsAsync<JObject>();
Sample sample = JsonConvert.DeserializeObject<Sample>(jObject.ToString());
}
您要收集这些数据的模型是
And you model to collect this data are
public class Item
{
public string test { get; set; }
}
public class Sample
{
public Item item { get; set; }
}
您将看到输出为
这篇关于如何从Postman到WebApi获取表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!