Multipart request with few parameters #1398
-
|
I don't think the following can be done: Because the Multipart takes two (at least) parameters I couldn't find a way to do it. Went through the full multipart documentation files. I always get the error : You can see in the attached file some lines of my code (only the important ones) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Hi! |
Beta Was this translation helpful? Give feedback.
-
|
Mixing simple form fields with file parts in a multipart request is supported. The method needs [Multipart] and every non-file parameter becomes a form field. The common mistake that produces blank fields is sending int/string parameters that the server cannot read as form fields, or not aliasing them to the exact field names the API expects. For your curl (scan_type=all plus a file field named file): [Multipart]
[Post("/api/v2/quick-scan/file")]
Task<ApiResult> QuickScan(
[AliasAs("scan_type")] string scanType,
[AliasAs("file")] StreamPart file);Call it: using var stream = File.OpenRead("Ziv.pdf");
await api.QuickScan("all", new StreamPart(stream, "Ziv.pdf", "application/pdf"));Set the api-key / user-agent / accept headers via a [Headers] attribute, a [Header] parameter, or a DelegatingHandler. Do not set Content-Type yourself for multipart - Refit sets multipart/form-data with the boundary automatically; setting it manually drops the boundary and the server then sees empty fields. For the case with two images (Avots): just add both as parts, each aliased to its server field name: [Multipart]
[Post("/upload")]
Task<HttpResponseMessage> UploadImage(
[AliasAs("id")] int id,
[AliasAs("typeId")] int typeId,
[AliasAs("orderId")] int orderId,
[AliasAs("photo")] ByteArrayPart photo,
[AliasAs("thumbnail")] ByteArrayPart thumbnail,
[Header("Authorization")] string authorization);If fields still arrive blank, capture the raw request with a logging DelegatingHandler and confirm the field names match exactly what the API validates. |
Beta Was this translation helpful? Give feedback.
Mixing simple form fields with file parts in a multipart request is supported. The method needs [Multipart] and every non-file parameter becomes a form field. The common mistake that produces blank fields is sending int/string parameters that the server cannot read as form fields, or not aliasing them to the exact field names the API expects.
For your curl (scan_type=all plus a file field named file):
Call it:
S…