Repository navigation
Post with file parameter order #2115
Description
DO NOT USE ISSUES FOR QUESTIONS
Describe the bug
when posting to an endpoint with File attachment, if there are other input parameters in the body, the File parameter is always render first regardless of the order the parameter is added.
To Reproduce
call an api endpoint with file attachment and additional parameter like fileName and fileType.
` using var fileServiceClient = GetFileServiceClient();
var request = new RestRequest("file/v1/files", Method.Post);
//added these parameters first
request.AddJsonBody(new FileUploadRequestModel { Type = "tps-volume-titles", FileName = fileName });
//then the file parameter
request.AddFile("file", fileContent, fileName, "text/csv");
request.AlwaysMultipartFormData = true;
var response = await fileServiceClient.PostAsync(request).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
_logger.LogError($"Failed to upload file {fileName}");
}
`
Expected behavior
Expected the http request to be:
POST https://somedomain.com/file/v1/files HTTP/1.1
Host: file-service.adsp-uat.alberta.ca
Authorization: Bearer eyxxx
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/110.2.0.0
Accept-Encoding: gzip, deflate, br
Content-Type: multipart/form-data; boundary="0a1f4db8-6981-4592-aff5-7b12b4e00197"
Content-Length: 115113
--0a1f4db8-6981-4592-aff5-7b12b4e00197
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=type
tps-volume-titles
--0a1f4db8-6981-4592-aff5-7b12b4e00197
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=filename
1680672668.csv
--0a1f4db8-6981-4592-aff5-7b12b4e00197
Content-Type: text/csv
Content-Disposition: form-data; name="file"; filename="1680672668.csv"
"xxx","2023-07-13 09:28:48",,ON00001,,"242006882001","S","*",,"2023-07-11","Subdivision Plan",,,"N",,"T","Fee Simple","242 006 882 +2",,,"2023-07-11"
--0a1f4db8-6981-4592-aff5-7b12b4e00197--
Actual rendered http request
POST https://somedomain.com/file/v1/files HTTP/1.1
Host: file-service.adsp-uat.alberta.ca
Authorization: Bearer eyxxx
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
User-Agent: RestSharp/110.2.0.0
Accept-Encoding: gzip, deflate, br
Content-Type: multipart/form-data; boundary="0a1f4db8-6981-4592-aff5-7b12b4e00197"
Content-Length: 115113
--0a1f4db8-6981-4592-aff5-7b12b4e00197
Content-Type: text/csv
Content-Disposition: form-data; name="file"; filename="1680672668.csv"
"xxx","2023-07-13 09:28:48",,ON00001,,"242006882001","S","*",,"2023-07-11","Subdivision Plan",,,"N",,"T","Fee Simple","242 006 882 +2",,,"2023-07-11"
--0a1f4db8-6981-4592-aff5-7b12b4e00197
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=type
tps-volume-titles
--0a1f4db8-6981-4592-aff5-7b12b4e00197
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=filename
1680672668.csv
--0a1f4db8-6981-4592-aff5-7b12b4e00197--
Stack trace
Desktop (please complete the following information):
- OS: windows
- .NET version 6
- Version 110.2.0
Additional context
Add any other context about the problem here.
Activity
b166er commentedon Jul 14, 2023
I have the same problem!
my first code, was very simple and naive. unfortunately it did not work as i expected:
output with Version 110.2.0 is:
my expected output was:
Here is my ugly code to set the order manually. It would be so great if the output of the parameters were in the same order as the executing Add-Methods, or at least some simple functionality to set the order, e.g. an order parameter.
koo9 commentedon Jul 14, 2023
just render the paramter by the order added would be sufficient. I believe you can set the file content type in the .AddFile() method but the overall content-type is still multipart/form-data
[-]Post with file parameter order bug[/-][+]Post with file parameter orde[/+][-]Post with file parameter orde[/-][+]Post with file parameter order[/+]alexeyzimarev commentedon Jul 15, 2023
Body parameters are added to the request message in the same order as they are added to
RestRequest. But, files are added separately, as well as POST parameters when the content is a multipart form.alexeyzimarev commentedon Jul 15, 2023
Duplicate of #2098