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

3

23

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.

Gipps answered 6/11, 2016 at 11:4 Comment(5)
Have a look at the following may need a Registry change as well [url-length-issue] (#15004732)Sipple
Thanks for the reply Mark. But can't these registry settings be overridden in the web.config file?Gipps
I believe your issue is that by default the maximum url segment length is 260 and this needs to be increased. This is controlled by the value in registry i believe called UrlSegmentMaxLength increa it to something larger, or work out a way to reduce the size of your url segments. A url segment is the amount of character between the slashes, in your case its alot !Sipple
is this a restfull api ? ...if yes , its better to go with a http post with content in the body if you having a large content.Picker
Replaced the url segment into a querystring parameter and it worked fine. Thought this is better compared to editing the registry.Gipps
R
29

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
Rieger answered 22/9, 2017 at 14:5 Comment(2)
It does, but some people are required to deal with it for whatever reason/requirement, so...Rieger
I owe you a beerHedger
W
15

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/

Wordsmith answered 8/2, 2018 at 12:52 Comment(2)
thanks for that, save my day... After many tries with web.config changes (which didn't fix it)Pied
awesome, thank you. nice clean solution in 30 secondsLiqueur
F
-1

I had the same issue. The URL character max length was defined in the HTTP.SYS registry and server is rebooted solved the issue.

Faithfaithful answered 26/4 at 3:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.