如何从 Postman To WebApi 获取表单数据

How to get form data from Postman To WebApi(如何从 Postman To WebApi 获取表单数据)
本文介绍了如何从 Postman To WebApi 获取表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Postman 接收表单数据:

I want to receive form data from Postman:

Content-Type: 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

这是 Postman 的第一个 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

这是 Postman 的第二个 tab

那么你的 API 将是

Then your API will be

[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

这是 Postman 的第三个 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 To WebApi 获取表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)