Page 1 of 1

TScHttpWebRequest - Exception

Posted: Wed 08 Dec 2021 14:03
by clement75
Hi,
I cannot get the exact server message of exception in the 2nd URL call. the below is my code.

1) First URL call no error:
try
Try
vHttpWebRequest := TScHttpWebRequest.Create(URL1);
vHttpWebRequest.Method := rmGET;
vHttpWebRequest.Accept := 'application/json';
vHttpWebRequest.ContentType := 'application/json';
vHttpWebRequest.KeepAlive := True;
vWebResponse := vHttpWebRequest.GetResponse;
vResponseStrRequest := vWebResponse.ReadAsString;
Except
on E : HttpException do
begin
vTmpStr := 'Error Code: ' + IntToStr(E.Code) + ' | ' +
'Server Message: ' + E.ServerMessage + ' | ' +
'Message: ' + E.Message;

raise Exception.Create(vTmpStr);
end;
on E: Exception do
begin
raise Exception.Create(E.Message);
end;
End;
Finally
vHttpWebRequest.Free;
end;

2) 2nd URL call has error, but cannot get the exact server message.
try
Try
vHttpWebRequest := TScHttpWebRequest.Create(URL2);
vHttpWebRequest.Method := rmPOST;
vHttpWebRequest.Accept := 'application/json';
vHttpWebRequest.ContentType := 'application/json';
vHttpWebRequest.KeepAlive := True;

vBuffer := TEncoding.UTF8.GetBytes(xxxxxxxxxxxxx);
vHttpWebRequest.ContentLength := Length(vBuffer);
vHttpWebRequest.WriteBuffer(vBuffer);
vWebResponse := vHttpWebRequest.GetResponse;
vResponseStrRequest := vWebResponse.ReadAsString;

Except
on E : HttpException do
begin
vTmpStr := 'Error Code: ' + IntToStr(E.Code) + ' | ' +
'Server Message: ' + E.ServerMessage + ' | ' +
'Message: ' + E.Message;

raise Exception.Create(vTmpStr);
end;
on E: Exception do
begin
raise Exception.Create(E.Message);
end;
End
Finally
vHttpWebRequest.Free;
end;

If i use Postman to test the URL2, it can return the server message 'Error: xxxxxxxxxxxxxxxxxx'.
However, if i run the my program to call the URL, it return other error message.
(if I only run the 2nd URL call, then can get the exact server message)
what is wrong in my code?

Thank you.

Re: TScHttpWebRequest - Exception

Posted: Thu 09 Dec 2021 03:13
by clement75
problem solved. ignore this topic

Re: TScHttpWebRequest - Exception

Posted: Thu 09 Dec 2021 11:24
by YanishevskiyVI
Glad to hear that your issue is resolved.
Feel free to contact us anytime in case you get any other questions.

Regards,