4
votes

I have created api project in .Net Core application. I have also created Method to accept HttpRequestMessage as parameter. Now I am trying to call my api method using Postman with including file as body parameter, but my api method is not calling.

Here is code

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpPost]
    public HttpResponseMessage PostFiles()
    {
        HttpResponseMessage result = null;
        var httpRequest = HttpContext.Request;

        return result;
    }}

Here is Postman request data

POST /api/values/PostFiles HTTP/1.1

Host: localhost:64226 Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW Cache-Control: no-cache Postman-Token: 6adcc652-a4ab-3714-cc5e-770bd214ac7a

------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="data"; filename="" Content-Type:

------WebKitFormBoundary7MA4YWxkTrZu0gW--

Here is the screen shot of my api call using postman.

enter image description here

Request body with file as parameter

enter image description here

Is there anything that I am missing?

Can you please help me out to call my web api with accepting file as parameter using Postman?

2

2 Answers

4
votes

I think there's a problem with your route. Try this :

public class ValuesController : Controller
{
    [Route("api/values/postfiles")]
    [HttpPost]
    public HttpResponseMessage PostFiles()
    {
        HttpResponseMessage result = null;
        var files = Request.Form.Files;
        return result;
    }
}

Then try accessing the API at http://localhost:yourport/api/values/postfiles .

replace "yourport" in your port no.

You'll get the file in "fileStream" if the file is available.

0
votes

In an ASP.NET Core application use IHostingEnvironment. Then you can call ContentRootPath and WebRootPath