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.
1 Answer
Reset to default 0I can no longer reproduce the exact problem I had.
However, related things to correct, to make it work:
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.)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 inWeb.ReqMulti
of Delphi 11.3, where it checksif PartName <> ''
. That seems like an unnecessary check to me, but that's they way Embarcadero has programmed it.)It seems suitable to use
function Upload: IMVCResponse;
instead ofprocedure Upload;
to be able to return a result, even though this is not strictly necessary.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744855086a4597356.html
评论列表(0条)