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\"
7
  • Since you are posting why don't you send the file as part of the body (a multipart body if possible)? Commented Jun 6, 2016 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. Commented Jun 6, 2016 at 20:03
  • Might be related to this: stackoverflow.com/questions/11728846/… Commented Jun 7, 2016 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. Commented Jun 7, 2016 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. Commented Jun 7, 2016 at 17:20

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.