0

I'm trying to send a string to my Web API. If I use a string literal for fileContents (adsf), it works:

        var config = {
            url: '/PrestoWeb/api/app/importTasks' + '?fileContents=adsf',
            method: 'POST'
        };

        $http(config)
            .then(function (response) {
                // success
            }, function (response) {
                // fail
            });

fileContents shows up correctly in the Web API:

    [AcceptVerbs("POST")]
    [Route("api/app/importTasks")]
    public Application ImportTasks(string fileContents)
    {
        // fileContents is adsf

If I change the url to my variable, I get 404 Not Found:

url: '/PrestoWeb/api/app/importTasks' + '?fileContents=' + fileContents,

I also tried this:

url: '/PrestoWeb/api/app/importTasks' + '?fileContents=' + encodeURIComponent(fileContents),

And this:

url: '/PrestoWeb/api/app/importTasks' + '?fileContents=' + encodeURIComponent(JSON.stringify(fileContents)),

Still get a 404 Not Found. What am I missing?

Note: The actual fileContents variable contains a string in XML format. Here is the first part of it:

"<ArrayOfTaskBase z:Id=\"1\" z:Type=\"System.Collections.Generic.List`1[[PrestoCommon.Entities.TaskBase, PrestoCommon, Version=3.6.0.0, Culture=neutral, PublicKeyToken=null]]\" z:Assembly=\"0\"
Bob Horn
  • 33,387
  • 34
  • 113
  • 219
  • Since you are posting why don't you send the file as part of the body (a *multipart* body if possible)? – Luiso Jun 06 '16 at 19:32
  • I tried that and couldn't get it working either, at first. Then I created a DTO and put my string in that object. It's working now by putting that DTO in the body of the request. I'd still like to know why I'm getting null in my original question. But folks think my question is too broad I guess. – Bob Horn Jun 06 '16 at 20:03
  • Might be related to this: http://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis – Dmitriy Khudorozhkov Jun 07 '16 at 15:37
  • 1
    What you are doing is bad practice. You should send the data in the message body, not the URI. The uri is handy for single valued short variables like ints, small strings, guids, datetimes, etc. You should not send a whole xml document in the uri. – Igor Jun 07 '16 at 16:24
  • 1
    While it may or may not be bad practice, perhaps he's stuck with this requirement. Bob, would you mind sharing the results of the encodeURIComponent? URI have a max length of about 2000 characters - make sure you're not exceeding that. Even better would be to provide the whole url result. – Zach Jun 07 '16 at 17:20
  • I got this working by putting the data in the message body. Then I tried it again just to see if @DmitriyKhudorozhkov's answer was right. I was still getting 404 Not Found. The issue ended up being what Zach suggested. My data was 2167 characters long. When I chose a shorter file to import, it worked. And I didn't need to encode it. Thank you all for the help. Zach, you deserve credit for the answer if you want to post it. – Bob Horn Jun 09 '16 at 14:48
  • Oh, and I'd love for someone to explain to me how this question is possibly "too broad." It couldn't be more specific. – Bob Horn Jun 09 '16 at 14:48

1 Answers1

2

Your fileContents string contains dots, rendering ASP.NET router unable to match the URL.

See that thread for the solution:

<add name="ApiURIs-ISAPI-Integrated-4.0"
     path="/PrestoWeb/api/app/*"
     verb="POST"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

(or, as Igor wrote in comments, just send the string in the request body)

Community
  • 1
  • 1
Dmitriy Khudorozhkov
  • 1,624
  • 12
  • 24