Helbo
Helbo

Reputation: 505

Web API Simplest way to pickup a multipart/form-data response

I have a problem but I cannot see what I'm doing wrong ? I want to make the simplest method possible to get this to work I will make it more complex later but currently just want the post method to be hit I don't even care if the code inside is working currently.

only thing I care about is that it sends a OK 200 reply back.

Currently I either get a internal server error 500 response or a 405 method not allowed response depending on if I try to send data or just force the method call with my browser.

have a IOS app that sends the post request below and I have a test PHP script that proved that this worked. Now I want to move it to the server that should be doing the work on a IIS 8. However I'm struggling with just even making the connection currently:

POST /api/Image/Upload HTTP/1.1
Host: ServerName
Content-Type: multipart/form-data; boundary=-------------Boundary----------35042EC1-790C-4CDD-8E21-66E6B1025C2D
Connection: keep-alive
Accept: */*
filename: DefaultPrefix1.jpg
User-Agent: KameraApp/1 CFNetwork/808.3 Darwin/16.3.0
Content-Length: 5492253
Accept-Language: da-dk
Accept-Encoding: gzip, deflate

---------------Boundary----------35042EC1-790C-4CDD-8E21-66E6B1025C2D
Content-Disposition: form-data; name='image'; filename="DefaultPrefix1.jpg"
Content-Type: image/jpg

Image Data
End boundary

How I'm trying to pick up the above Multipart/form-data:

[RoutePrefix("api/Image")]
public class ImageController : ApiController
    {

        [HttpPost]
        [Route("Upload")]
        public IHttpActionResult Upload()
        {
            var files= HttpContext.Current.Request.Files;

            var aFile = files.Get(0);


            string filename = aFile.FileName;
            string path = @"C:\ImageUploaderService\UploadFolder";
            aFile.SaveAs(path + filename);
            return Ok("Working");
        }
    }

Edit:

changed to the current Method based on the suggestion below

my WebApiConfig looks like this:

  public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

Upvotes: 2

Views: 4056

Answers (1)

Nkosi
Nkosi

Reputation: 247153

This is a routing issue. If the intention is to use attribute routing, then make sure that it is enabled

public static class WebApiConfig {
    public static void Register(HttpConfiguration config) {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

And decorate the target controller accordingly

[RoutePrefix("api/Image")]
public class ImageController : ApiController {        
    [HttpPost]
    [Route("Upload")] // Matches POST api/Image/Upload
    public IHttpActionResult Upload()
    {
        return Ok("I am working");
    }
}

From there you should be able to include your additional functionality.

If you want to read multipart data consider getting it from the request's content.

var content = await Request.Content.ReadAsMiltipartAsync();

only thing I care about is that it sends a OK 200 reply back.

I want to make the simplest method possible to get this to work I will make it more complex later but currently just want the post method to be hit I don't even care if the code inside is working currently.

This answer should address the above. Any further issues should be posted in other questions.

Upvotes: 3

Related Questions