Jump to content
drazde

THttpCli get request fail with 404 Bad Request result

Recommended Posts

Hi,

I use THttpCli component (ICS v8.61) for downloading files.

It works well in many case, but when I try pass a parameter with space (encoding with "+" or "%20") I have the error 400 Bad Request

 

Why?

What can I do?

 

PS: if I put the url in a browser, the request is successful

text/html; charset=iso-8859-1 => CONSEGNA Attestazione avvenuta pubblicazione avviso selezione infermiere.eml
Location = http://192.168.2.221/cbadoc/upload/2019/08/14/CONSEGNA Attestazione avvenuta pubblicazione avviso selezione infermiere.eml
URL = http://192.168.2.221/cbadoc/download.php?file=CONSEGNA+Attestazione+avvenuta+pubblicazione+avviso+selezione+infermiere.eml&id=20878&liv1=2019&liv2=08&liv3=14
Document = CONSEGNA Attestazione avvenuta pubblicazione avviso selezione infermiere.eml
RequestDone, no error. Status = 400
Request Failed !
StatusCode   = 400
ReasonPhrase = Bad Request

 

Share this post


Link to post

The server has decided it does not like your parameter encoding, but you don't say you how you do it.   Generally, you encode to UTF8 first, then use IcsUrlEncode for each parameter, then combine the parameters.  But there are variations on URL encoding, the latest beta has a new strict parameter for Twitter which is very fussy. 

 

The better way is to change your application to use TSslHttpRest component, which has RestParams that do all this stuff for you, mostly correctly.  . But update to at least V8.64 first, or even the overnight zip, there have been a lot of changes this year. 

 

Angus

 

Share this post


Link to post

Since it is plain http you can use Wireshark to check, what is different in network request comparing browser and you program.

Share this post


Link to post

+1 for comparing to browser. Moreover, you don't even need Wireshark, just the browser's Dev tools with which you even can modify requests before sending.

Share this post


Link to post

I know, this isn't a good solution, but with TDownloadURL component I don't have this problem in this cases....

I use this component only for downloading file that have params with space and all other with THttpCli.

 

I don't want use always TDownloadURL because it have many other problems...

Share this post


Link to post

THttpCli does not encode any parameters, you added that coding, so if the URL does not work you need to correct your code as I suggested yesterday.

 

Angus

 

Share this post


Link to post
4 hours ago, Angus Robertson said:

THttpCli does not encode any parameters, you added that coding, so if the URL does not work you need to correct your code as I suggested yesterday.

 

Angus

 

I have update ICS component with the latest (v8.65), I use UTF8Encode and then IcsUrlEncode (on the filename parameter the other are merely numbers) but nothing...

 

I remain the better solution is pass the parameters in post (not in get like now), so I think I will talk with my colleague for change it.

Thanks for your answers

Share this post


Link to post

URL encoding for GET and POST is the same,  although POST may accept Json or XML instead. 

 

Don't like + for space, should always be %20. The + originates from browser form fields with application/x-www-form-urlencoded MIME POST content, and should not be used in URLs or parameters.

 

Angus

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×