为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
忽然笑 2021-11-14 17:21:18
我想从 Postman 接收表单数据:Content-Type: application/json这是 WebApi 方法:[HttpPost][Route("api/test")]public async Task TestMethod(HttpRequestMessage request){    var test = await request.Content.ReadAsStringAsync();}我得到的是:------WebKitFormBoundarypqDvmeG89cBR9mK9Content-Disposition: form-data; name="test"esad------WebKitFormBoundarypqDvmeG89cBR9mK9--但是我不想要数据,WebKitFormBoundary并且我限制只能使用 formdata。有没有其他办法?HTTP调用信息:POST /api/test HTTP/1.1Host: localhost:16854Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gWCache-Control: no-cachePostman-Token: 1a3d6427-4956-707d-da0c-3a29a63c7563------WebKitFormBoundary7MA4YWxkTrZu0gWContent-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 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

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

这是tab邮递员的第一个

//img1.sycdn.imooc.com//6190d5300001798a06900243.jpg

如果您只需要收集您发布的一个键/值对 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邮递员的第二个

//img1.sycdn.imooc.com//6190d53f0001370f06910232.jpg

那么你的 API 将是


[HttpPost]

[Route("api/test")]

public async Task TestMethod(HttpRequestMessage request)

{

    var test = await request.Content.ReadAsFormDataAsync();

}

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

//img1.sycdn.imooc.com//6190d54c00014b2404880139.jpg

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

这是tab邮递员的第三个

有关此类选项,请参见下面的屏幕截图

//img1.sycdn.imooc.com//6190d5580001c97806070183.jpg

你的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; }

}

你的输出将是

//img1.sycdn.imooc.com//6190d5670001cba005490197.jpg

此选项的优点是您可以将其complex type作为发布的数据发送等

//img1.sycdn.imooc.com//6190d57200016aa007360160.jpg

你的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; }

}

你会看到输出是

//img1.sycdn.imooc.com//6190d57f000103dc06100202.jpg

查看完整回答
反对 回复 2021-11-14
?
繁花不似锦

TA贡献1851条经验 获得超4个赞

从 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);

}

可以在此处找到更详尽的示例(部分:阅读表单控件数据)。


编辑:发送到上述 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--


查看完整回答
反对 回复 2021-11-14
?
茅侃侃

TA贡献1842条经验 获得超21个赞

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

NameValueCollection form = HttpContext.Current.Request.Form;


查看完整回答
反对 回复 2021-11-14
  • 3 回答
  • 0 关注
  • 253 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信