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

时间:2018-08-04 08:21:37

标签: c# asp.net asp.net-web-api postman form-data

我想从邮递员那里接收表格数据:

enter image description here

Content-Type: application/json

这是WebApi方法:

[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。还有其他办法吗?

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 

3 个答案:

答案 0 :(得分:8)

1)如果您必须发送Content-Type: multipart/form-data或只需发送form-data

这是邮递员的第一个tab

enter image description here

如果您只需要收集发布的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

中的多个键/值对
[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

这是邮递员的第二个tab

enter image description here

那么您的API将是

[HttpPost]
[Route("api/test")]
public async Task TestMethod(HttpRequestMessage request)
{
    var test = await request.Content.ReadAsFormDataAsync();
}

然后,当您使用断点调试代码时,您将获得以下输出

enter image description here


3)如果您必须发送Content-Type: application/json

这是邮递员的第三个tab

请参见下面的屏幕快照以了解该选项

enter image description here

您的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());
}

您的模型可以收集发布的数据

public class Item
{
    public string test { get; set; }
}

您的输出将是

enter image description here

此选项的优点是您可以发送complex type作为发布的数据,就像

enter image description here

您的api是

[HttpPost]
[Route("test")]
public async Task TestMethod(HttpRequestMessage request)
{
    var jObject = await request.Content.ReadAsAsync<JObject>();

    Sample sample = JsonConvert.DeserializeObject<Sample>(jObject.ToString());
}

您要收集这些数据的模型是

public class Item
{
    public string test { get; set; }
}

public class Sample
{
    public Item item { get; set; }
}

您将看到输出为

enter image description here

答案 1 :(得分:1)

从Postman发送并选择了form-data选项时,以下代码将正确读取键/值

[HttpPost]
[Route("api/test")]
public async Task<HttpResponseMessage> TestMethod(HttpRequestMessage request)
{
    string root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);

    await Request.Content.ReadAsMultipartAsync(provider);

    var testValue = provider.FormData.GetValues("test")[0];

    return Request.CreateResponse(HttpStatusCode.OK);
}

可以找到更详尽的示例here(部分:读取表单控制数据)。

编辑:发送到上述API处理程序的HTTP调用如下:

POST /api/stats/testmethod HTTP/1.1
Host: localhost:4100
Cache-Control: no-cache
Postman-Token: 999fd13d-f804-4a63-b4df-989b660bcbc5
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="test"

esad
------WebKitFormBoundary7MA4YWxkTrZu0gW--

答案 2 :(得分:0)

下面的语句可以做到这一点:

NameValueCollection form = HttpContext.Current.Request.Form;