Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get a 400 bad request - invalid Url when the length of the request exceeds 320+ characters

Not sure what could be the reason. Added the following in Web.config also. maxUrlLength="1024" maxQueryStringLength="1024" requestPathInvalidCharacters="" maxRequestLength="1024" relaxedUrlToFileSystemMapping="true" I encode the uri parameter and send the request but get a invalid url all the time. Url: http://localhost:51840/api/xxxxxxx/xxxxxxxxxxxx/n4cEF3OM0LC0q8I6OMe0XdHo8evXnoeyC06A%252fL%252fu0a%252f6e0RQrdxl2%252fPkSKNfh4aEQm78TReX1zIfGDB1bgH%252bGBEI%252fBw6i0DwQ6%252b3dk6yjs1UZqqICQye6QqVX8bJT%252fBK7GE8a22FeXJiPVtEgFRmp4WcHc4pIRHaE6QGK28kiASGFfUC9tDdRZhLfmYJsluGpsseSUOc5Inxlf3fTFNe7sg7gixrLTlwhfJIATZWWsPc%252bfo6BITTFA%253d%253d

If I reduce the url to the following it works fine. http://localhost:51840/api/xxxxxxx/xxxxxxxxxxxx/n4cEF3OM0LC0q8I6OMe0XdHo8evXnoeyC06A%252fL%252fu0a%252f6e0RQrdxl2%252fPkSKNfh4aEQm78TReX1zIfGDB1bgH%252bGBEI%252fBw6i0DwQ6%252b3dk6yjs1UZqqICQye6QqVX8bJT%252fBK7GE8a22FeXJiPVtEgFRmp4WcHc4pIRHaE6QGK28kiASGFfUC9tDdRZhLfmYJsluGpsseSUOc5Inxlf3fTFNe7sg7gixrLTlwhfJIATZWWsPc%252bfo6BI

When the last 10+ characters it gives me a bad request. Any help greatly appreciated.

like image 362
Vignesh T Avatar asked Nov 06 '16 11:11

Vignesh T


People also ask

What is the reason for 400 Bad Request?

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

When can I return a Bad Request?

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). A business rule doesn't fall under any of those three examples.


2 Answers

Just documenting my experience:

My issue was the one pointed by @Mark at the question comments:

"by default the maximum url segment length is 260"

He suggested to increase this limit in the registry, but I didn't want this kind of trouble. The suggestion by @Vignesh T worked better:

"Replaced the url segment into a querystring parameter and it worked fine"

In summary, instead of doing:

http://path.to.website/very-long-string-goes-here/

I just did:

http://path.to.website/?key=very-long-string-goes-here
like image 199
Renato Byrro Avatar answered Sep 24 '22 13:09

Renato Byrro


run in powershell

Set-ItemProperty -Path HKLM:\System\CurrentControlSet\Services\HTTP\Parameters -Name UrlSegmentMaxLength -Value 2000 -Type "Dword"

then do net stop http and net start http.

https://blogs.msdn.microsoft.com/amyd/2014/02/06/response-400-bad-request-on-long-url/

like image 45
Toolkit Avatar answered Sep 24 '22 13:09

Toolkit