How to get form data from Postman To WebApi

Hina Khuman picture Hina Khuman · Aug 4, 2018 · Viewed 17.5k times · Source

I want to receive form data from Postman:

enter image description here

Content-Type: application/json

Here is WebApi method:

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

What I'm getting is:

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

esad
------WebKitFormBoundarypqDvmeG89cBR9mK9--

But I don't want data with WebKitFormBoundary and I've restriction to use formdata only. Is there any other way?

HTTP call information:

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 call information:

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 

Answer

er-sho picture er-sho · Aug 4, 2018

1) If you have to send Content-Type: multipart/form-data OR simply form-data

This is the first tab of Postman

enter image description here

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

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) If you have to send Content-Type: application/x-www-form-urlencoded

This is the second tab of Postman

enter image description here

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

enter image description here


3) If you have to send Content-Type: application/json

This is the third tab of Postman

See below screenshot for such option

enter image description here

And your api is

[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; }
}

And your output will be

enter image description here

The advantage of this option you can send complex type as posted data and like

enter image description here

And your api is

[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; }
}

And you will see the output is

enter image description here