VisualBean
VisualBean

Reputation: 5008

Restful post throws 400 invalid url when reaching 295 chars

I've created a restful webservice with Web-Api.

I'm trying to do a post at this url

../api/AAEAAAD_____AQAAAAAAAAAMAgAAAEVPYmplY3RUb0Jhc2U2NCwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGwFAQAAABlPYmplY3RUb0Jhc2U2NC5DcmVkZW50aWFsAgAAABk8VXNlcm5hbWU-a19fQmFja2luZ0ZpZWxkGTxQYXNRmllbGQBAQIAAAAGAwAAAA5hd2NhQGF0ZWEtYW5jdAYEAAAAC0czcnRtNG5zMGZ0CwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2/say

The base64 is encoded with

HttpServerUtility.UrlTokenEncode();

I get a "HTTP Error 400. The request URL is invalid." when trying to do a post.

I've tried setting maxUrlLength as I've seen a few others with the same type of problem, alas, this did not help.

So far, I've tried

nothing has worked so far. I've found the magic number to be 294 allowed chars in the full url meaning -> If I cut of some of the characters from the long string until i get to 294 characters, everything works as intented, as to why certain it's not a routing problem nor a problem with my post method

Any good ideas as to what can be the issue?

For anyone trying to achieve the same thing I'm trying - Heres my route

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

and my Post method

public string Post(string key)
    {
       if(ConvertFromBase64(key))
       {
       //Do stuff
       }
    }

Upvotes: 4

Views: 1068

Answers (2)

malix
malix

Reputation: 3572

Try with the key in the query string instead?

public class SomeController : Controller
    [Route("api/say")]
    public ActionResult Say(string key) {
    }

With a url like

../api/say?key=AAEAAAD_____AQAAAAAAAAAMAgAAAEVPYmplY3RUb0Jhc2U2NCwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGwFAQAAABlPYmplY3RUb0Jhc2U2NC5DcmVkZW50aWFsAgAAABk8VXNlcm5hbWU-a19fQmFja2luZ0ZpZWxkGTxQYXNRmllbGQBAQIAAAAGAwAAAA5hd2NhQGF0ZWEtYW5jdAYEAAAAC0czcnRtNG5zMGZ0CwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2

Upvotes: 1

Francis Ducharme
Francis Ducharme

Reputation: 4987

I know you tried through the web.config file, but can you try to increase the maxLength of the key parameter, like such ?

config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{key:maxlength(500)}/{controller}/{id}", //whatever is the max length of your parameter...
        defaults: new { key=RouteParameter.Optional,id = RouteParameter.Optional }
        );

Upvotes: 0

Related Questions