0

Good morning, I am facing the following issue when sending this request, which requires a Multipart/form-data with one parameter receiving a JSON and another receiving the file. Currently, this is the code I use to make the request.

`public async Task<ResponseCreatedDocument> AddDocumentFile(ResponseLogin rest, string idcabinet,string docguid,  AddFileDocumentModel addfile, string filePath)
{
    try
    {
        var fileInfo = new FileInfo(filePath);
        byte[] fileBytes = File.ReadAllBytes(filePath);
        var request = new RestRequest($"{idcabinet}/{docguid}/addFile", Method.Post);
        request.RequestFormat = DataFormat.Json;
        request.AddHeader("Authorization", $"Bearer {rest.Token}");
        request.AddHeader("accept", "*/*");
        request.AlwaysMultipartFormData = true;
        string addFileParamsJson = JsonConvert.SerializeObject(addfile );
        request.AddParameter("AddFileParams", addFileParamsJson, ParameterType.RequestBody);
        string contentType = GetMimeType(fileInfo.Extension);
        request.AddFile("FileData", fileBytes, fileInfo.Name, contentType);
        var response = client.Execute(request);

        Console.WriteLine("{0}", response.Content);
        ResponseCreatedDocument doc = new ResponseCreatedDocument();
        doc = JsonConvert.DeserializeObject<ResponseCreatedDocument>(response.Content);
        return doc;
        
    }
    catch (Exception ex)
    {
        Console.WriteLine("An error occurred: {0}", ex.Message);
        throw;
    }
}
private static string GetMimeType(string ext)
{
    switch ((ext ?? string.Empty).ToLowerInvariant())
    {
        case ".pdf": return "application/pdf";
        case ".png": return "image/png";
        case ".jpg":
        case ".jpeg": return "image/jpeg";
        case ".txt": return "text/plain";
        default: return "application/octet-stream";
    }
}`

But when I execute the request, the response returns a bad request with the following error message. : {"Message":"Invalid parameter 'AddFileParams'. - Empty value\r\nParameter name: AddFileParams"} As if it isn't reading the parameter I'm sending, thank you very much in advance.

I already tried placing the string directly hardcoded into it. request.AddParameter But I keep getting the same result; what advice or fix should I apply to my code lines?

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.