Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TMVCWebRequest.ContentParam issue with Indy multipart/form-data request #758

Open
aigazy opened this issue Jun 19, 2024 · 3 comments
Open
Assignees
Labels
as-designed The behaviour is correct/expected

Comments

@aigazy
Copy link

aigazy commented Jun 19, 2024

Hello.

I have found that when I use Indy component to send a multipart/form-data request to the dmvcframework server, TMVCWebRequest.ContentParam function couldn't return the value of the content parameter.

I send a request using the TIdHTTP component, adding a form field as following:
FFormData.AddFormField(AField, AValue, 'utf-8').ContentTransfer := '8bit';
Because I need to support UTF8 symbols in my endpoint.

I logged a request on the server side.
Content type:
multipart/form-data;boundary=--------061924103740983

Request body:

----------061924103740983
Content-Disposition: form-data; name="organization_id"
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

5
----------061924103740983
Content-Disposition: form-data; name="first_name"
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Асқар
----------061924103740983
Content-Disposition: form-data; name="last_name"
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Ахмедов
----------061924103740983
Content-Disposition: form-data; name="job"
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

Тестер
----------061924103740983
Content-Disposition: form-data; name="registered_at"
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

2024-06-19 10:37:40
----------061924103740983

When I try to get the content param value, it returns empty string:
var Param := Context.Request.ContentParam('organization_id')
So I think that content param parser not working properly here.

@aigazy
Copy link
Author

aigazy commented Jun 25, 2024

@danieleteti could you please take a look at this issue?

@danieleteti danieleteti self-assigned this Jun 26, 2024
@danieleteti
Copy link
Owner

Why use a multipart/form-data when you actually need a application/x-www-form-urlencoded? (at least in the sample you wrote)

Send the params as follows:

  var lForm := TStringList.Create;
  try
    lForm.Values['field1'] := 'value1';
    lForm.Values['field2'] := 'value2';
    IdHTTP1.Post('http://localhost:8080/api/myformdata', lForm);
  finally
    lForm.Free;
  end;

and read params as follows:

type
  [MVCPath('/api')]
  TMyController = class(TMVCController)
  public
    [MVCPath('/myformdata')]
    [MVCHTTPMethod([httpPOST])]
    [MVCConsumes(TMVCMediaType.APPLICATION_FORM_URLENCODED)]
    function ReceiveFormData: IMVCResponse;
  end;

implementation

function TMyController.ReceiveFormData: IMVCResponse;
begin
  LogI(Context.Request.ContentParam('field1'));
  LogI(Context.Request.ContentParam('field2'));
end;

You can also use MVCFromContentField attribute to read content fields directly from action parameters.

@danieleteti danieleteti added the as-designed The behaviour is correct/expected label Jun 26, 2024
@aigazy
Copy link
Author

aigazy commented Jun 27, 2024

Because I need to send also files using multipart/form-data. Just try to send multipart/form-data request from the Indy component to the dmvcframework api server. If you will use a following charset and content transfer params:

FFormData.AddFormField(AField, AValue, 'utf-8').ContentTransfer := '8bit';

Server could not parse content params from the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as-designed The behaviour is correct/expected
Projects
None yet
Development

No branches or pull requests

2 participants