delphi - DelphiMVCFramework: "Route not found" after adding MVCConsumes(TMVCMediaType.MULTIPART_FORM_DATA) - S

I am trying to write an endpoint handler in DelphiMVCFramework that accepts upload of a binary file. I

I am trying to write an endpoint handler in DelphiMVCFramework that accepts upload of a binary file. I use Delphi 11.3 and dmvcframework-3.4.2-magnesium, which otherwise works fine.

However, as soon as I add [MVCConsumes(TMVCMediaType.MULTIPART_FORM_DATA)], it's as if the whole endpoint disappears.

This is my code:

type
  [MVCPath('/api')]
  TMyController = class(TMVCController)
  public
    [MVCPath('/upload')]
    [MVCHTTPMethod([httpPOST])]
    [MVCConsumes(TMVCMediaType.MULTIPART_FORM_DATA)]
    procedure Upload;
  end;

This is the resulting log when localhost:8080/api/upload is called:

2025-03-10 10:43:23:559 [TID    22260][INFO   ] Listening on http://localhost:8080
2025-03-10 10:43:23:559 [TID    22260][INFO   ] Application started. Press Ctrl+C to shut down.
2025-03-10 10:43:31:567 [TID    16204][INFO   ] Loading Config default values
2025-03-10 10:43:31:567 [TID    16204][INFO   ] Loading System Controllers
2025-03-10 10:43:31:569 [TID    16204][WARNING] POST:/api/upload [0:0:0:0:0:0:0:1] -> {ROUTE NOT FOUND} - 404

I use Postman to make the POST call, reusing a call I've done earlier to a another server (not DMVCFramework-based), which is working, i.e. the Postman call should be OK, I believe.

What might cause the problem in DelphiMVCFramework and how do I solve it? If I remove [MVCConsumes(TMVCMediaType.MULTIPART_FORM_DATA)], the endpoint starts working, but will of course not be usable for what I want.

I am trying to write an endpoint handler in DelphiMVCFramework that accepts upload of a binary file. I use Delphi 11.3 and dmvcframework-3.4.2-magnesium, which otherwise works fine.

However, as soon as I add [MVCConsumes(TMVCMediaType.MULTIPART_FORM_DATA)], it's as if the whole endpoint disappears.

This is my code:

type
  [MVCPath('/api')]
  TMyController = class(TMVCController)
  public
    [MVCPath('/upload')]
    [MVCHTTPMethod([httpPOST])]
    [MVCConsumes(TMVCMediaType.MULTIPART_FORM_DATA)]
    procedure Upload;
  end;

This is the resulting log when localhost:8080/api/upload is called:

2025-03-10 10:43:23:559 [TID    22260][INFO   ] Listening on http://localhost:8080
2025-03-10 10:43:23:559 [TID    22260][INFO   ] Application started. Press Ctrl+C to shut down.
2025-03-10 10:43:31:567 [TID    16204][INFO   ] Loading Config default values
2025-03-10 10:43:31:567 [TID    16204][INFO   ] Loading System Controllers
2025-03-10 10:43:31:569 [TID    16204][WARNING] POST:/api/upload [0:0:0:0:0:0:0:1] -> {ROUTE NOT FOUND} - 404

I use Postman to make the POST call, reusing a call I've done earlier to a another server (not DMVCFramework-based), which is working, i.e. the Postman call should be OK, I believe.

What might cause the problem in DelphiMVCFramework and how do I solve it? If I remove [MVCConsumes(TMVCMediaType.MULTIPART_FORM_DATA)], the endpoint starts working, but will of course not be usable for what I want.

Share asked Mar 10 at 8:55 Matthias BMatthias B 1,1179 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I can no longer reproduce the exact problem I had.

However, related things to correct, to make it work:

  1. Using unit Web.ReqMulti is required according to DelphiMVCFramework samples. (However, it did not seem to matter to me, as it seems to be automatically included by DelphiMVCFramework itself.)

  2. Web.ReqMulti cannot handle file attachments that don't have a name, i.e. what Postman calls "key". I had left it empty. As soon as I fill it in with any dummy value (e.g. "x"), I can access the files in the Upload handler. (It is about line 257 in Web.ReqMulti of Delphi 11.3, where it checks if PartName <> ''. That seems like an unnecessary check to me, but that's they way Embarcadero has programmed it.)

  3. It seems suitable to use function Upload: IMVCResponse; instead of procedure Upload; to be able to return a result, even though this is not strictly necessary.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744855086a4597356.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信