danieleteti / delphimvcframework

DMVCFramework (for short) is a popular and powerful framework for WEB API in Delphi. Supports RESTful and JSON-RPC WEB APIs development.
Apache License 2.0
1.23k stars 356 forks source link

Querystring parameter is placed in context.request.body - but only on GET, DELETE and OPTIONS #715

Closed Microcom-Bjarne closed 10 months ago

Microcom-Bjarne commented 10 months ago

Hi.

Using latest - DMVC :) on Delphi 11.3

I don't understand why I get my querystring parameters placed inside my body part when using GET, DELETE or OPTIONS. I do not use OPTIONS anywhere - but I tested it, since I could :)

Slightly modifying the MiddlewareSamples.dpr sample application.

Consider this controller endpoint.

    [MVCPath('/qsp')]
    [MVCHTTPMethod([httpPOST, httpGET, httpDELETE, httpPUT, httpTRACE, httpPATCH, httpHEAD, httpOPTIONS])]
    procedure Index_qsp([MVCFromQueryString('par1')] const par1:boolean);

with this implementation.

procedure TApp1MainController.Index_qsp([MVCFromQueryString] const par1:boolean);
begin
  ContentType := TMVCMediaType.TEXT_PLAIN;
  Render(Context.Request.Body);
end;

And using postman and calling the endpoint with the allowed verbs results in:

GET : par1=true DELETE : par1=true OPTIONS : par1=true POST : PUT : TRACE : PATCH : HEAD :

I tried the app - without the middleware. Same result. So it has nothing to do with the middleware. I also tested with pathparams - and all verbs return empty body, as I would expect.

I'm building my own middleware for logging purpose. And it broke on some occasions where I used querystring parameters. And that is why I started looking into why it broke.

Anyone know if this is expected behaviour? Is it coming from INDY or is it a bug i DMVC?

danieleteti commented 10 months ago

This is the normal INDY behavior: the "body" of the method which could provide a body but don't, returns the querystring parameters as key/value strings pair. Do not rely on this because a different engine could have a different behaviour. Please follow the Robustness Principle.