The variable “Resultado” always takes the value: “ERROR: java.net.ProtocolException: method does not support a request body: GET”. It seems that sugar HttpRequest object is not handlig correctly the mode “HttpRequestMode.Post” (I get the same error if I try to use a GET method: Request.Mode = HttpRequestMode.Get)
Having looked at the (current) Sugar code for handling the request it’s not obvious to me how the Post request mode might end up incorrectly handled as a Get. Which raises the possibility that the setting of the Request.Mode itself is not working as expected.
Have you checked the value of Request.Mode at the point just prior to the request being performed ?
Prior to executing Http.GetString the value of Request.Mode is the one assigned: HttpRequestMode.Post.
I forgot to mention that I’ve also checked current Sugar code, and it seems to be right. So, I tried to use this current version, but I keep getting the same error… The code of my post is the simplest one that throws the error, but none of the methods of Request object works to retrieve data when I set HttpRequestMode.Post…
A lot of years has passed since last time I coded in Pascal… but today I’m lucky!
It seems that the problem is in the method Http.ExecuteRequestSynchronous (file HTTP.bas of project Sugar.Shared). I also suppose that the method Http.ExecuteRequest has the same problem. The code of this methods begins with:
{$IF COOPER}
var lConnection := java.net.URL(aRequest.Url).openConnection as java.net.HttpURLConnection;
for each k in aRequest.Headers.Keys do
lConnection.setRequestProperty(k, aRequest.Headers[k]);
//
// OPS: Here lConnection "thinks" that method to use is 'GET'...
//
if assigned(aRequest.Content) then begin
lConnection.getOutputStream().write((aRequest.Content as IHttpRequestContent).GetContentAsArray());
lConnection.getOutputStream().flush();
end;
...
I think that the solution is to “tell” lConnection which is the method to use to do the request, adding this code in the marked line (the “OPS” line):
case aRequest.Mode of
HttpRequestMode.Get: lConnection.setRequestMethod('GET');
HttpRequestMode.Post: lConnection.setRequestMethod('POST');
HttpRequestMode.Head: lConnection.setRequestMethod('HEAD');
HttpRequestMode.Put: lConnection.setRequestMethod('PUT');
HttpRequestMode.Delete: lConnection.setRequestMethod('DELETE');
HttpRequestMode.Patch: lConnection.setRequestMethod('PATCH');
HttpRequestMode.Options: lConnection.setRequestMethod('OPTIONS');
HttpRequestMode.Trace: lConnection.setRequestMethod('TRACE');
end;
A code similar to this is used for ECHOES and TOFFEE, so instead of repeating this code fragment 6 times in this code module (HPPT.pas), I suggest to define a function to “translate” the value of aRequest.Mode to a string code (for example, HttpRequestMode2String). With this function, the correction takes only one line of code:
Looks right to me. Confession: Somehow the #ifdef’s (the requestmode handling being only for the ECHOES impl.) didn’t register when I was reading the code.