猿问

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

我想从 Postman 接收表单数据:

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 


忽然笑
浏览 257回答 3
3回答

幕布斯7119047

1)如果您必须发送Content-Type: multipart/form-data或只是form-data这是tab邮递员的第一个如果您只需要收集您发布的一个键/值对 form-data[HttpPost][Route("api/test")]public HttpResponseMessage TestMethod(HttpRequestMessage request){&nbsp; &nbsp; var testValue = HttpContext.Current.Request.Form["test"];&nbsp; &nbsp; return Request.CreateResponse(HttpStatusCode.OK, testValue);}如果您必须收集多个已发布的键/值对 form-data[HttpPost][Route("api/test")]public HttpResponseMessage TestMethod(HttpRequestMessage request){&nbsp; &nbsp; NameValueCollection collection = HttpContext.Current.Request.Form;&nbsp; &nbsp; var items = collection.AllKeys.SelectMany(collection.GetValues, (k, v) => new { key = k, value = v });&nbsp; &nbsp; //We just collect your multiple form data key/value pair in this dictinary&nbsp; &nbsp; //The following code will be replaced by yours&nbsp; &nbsp; Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();&nbsp; &nbsp; foreach (var item in items)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; keyValuePairs.Add(item.key, item.value);&nbsp; &nbsp; }&nbsp; &nbsp; return Request.CreateResponse(HttpStatusCode.OK, keyValuePairs);}2)如果你必须发送 Content-Type: application/x-www-form-urlencoded这是tab邮递员的第二个那么你的 API 将是[HttpPost][Route("api/test")]public async Task TestMethod(HttpRequestMessage request){&nbsp; &nbsp; var test = await request.Content.ReadAsFormDataAsync();}然后当您使用断点调试代码时,您将获得以下输出3)如果你必须发送&nbsp;Content-Type: application/json这是tab邮递员的第三个有关此类选项,请参见下面的屏幕截图你的api是[HttpPost][Route("api/test")]public async Task TestMethod(HttpRequestMessage request){&nbsp; &nbsp; &nbsp;var jObject = await request.Content.ReadAsAsync<JObject>();&nbsp; &nbsp; &nbsp;Item item = JsonConvert.DeserializeObject<Item>(jObject.ToString());}以及您收集这些发布数据的模型public class Item{&nbsp; &nbsp; public string test { get; set; }}你的输出将是此选项的优点是您可以将其complex type作为发布的数据发送等你的api是[HttpPost][Route("test")]public async Task TestMethod(HttpRequestMessage request){&nbsp; &nbsp; var jObject = await request.Content.ReadAsAsync<JObject>();&nbsp; &nbsp; Sample sample = JsonConvert.DeserializeObject<Sample>(jObject.ToString());}你收集这些数据的模型是public class Item{&nbsp; &nbsp; public string test { get; set; }}public class Sample{&nbsp; &nbsp; public Item item { get; set; }}你会看到输出是

繁花不似锦

从 Postman 发送并form-data选择选项时,以下代码将正确读取键/值[HttpPost][Route("api/test")]public async Task<HttpResponseMessage> TestMethod(HttpRequestMessage request){&nbsp; &nbsp; string root = HttpContext.Current.Server.MapPath("~/App_Data");&nbsp; &nbsp; var provider = new MultipartFormDataStreamProvider(root);&nbsp; &nbsp; await Request.Content.ReadAsMultipartAsync(provider);&nbsp; &nbsp; var testValue = provider.FormData.GetValues("test")[0];&nbsp; &nbsp; return Request.CreateResponse(HttpStatusCode.OK);}可以在此处找到更详尽的示例(部分:阅读表单控件数据)。编辑:发送到上述 API 处理程序的 HTTP 调用如下:POST /api/stats/testmethod HTTP/1.1Host: localhost:4100Cache-Control: no-cachePostman-Token: 999fd13d-f804-4a63-b4df-989b660bcbc5Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW------WebKitFormBoundary7MA4YWxkTrZu0gWContent-Disposition: form-data; name="test"esad------WebKitFormBoundary7MA4YWxkTrZu0gW--

茅侃侃

下面的语句可以做到这一点:NameValueCollection&nbsp;form&nbsp;=&nbsp;HttpContext.Current.Request.Form;
随时随地看视频慕课网APP
我要回答