Search code examples
c#httpmultipartform-datahttp-status-code-400refit

C# Refit Multipart Content stays blank


I'm new to C# and Refit and I encountered an issue I can't solve. I need to convert the following CURL request to Refit (It must be refit)

curl -X 'POST' \
  'https://www.hybrid-analysis.com/api/v2/quick-scan/file' \
  -H 'accept: application/json' \
  -H 'user-agent: Mozilla 5.0' \
  -H 'api-key: p36a6qk6119e21dfxf2repsy918df0b2mt5cifpra922df944eg2iim292b1f961' \
  -H 'Content-Type: multipart/form-data' \
  -F 'scan_type=all' \
  -F '[email protected];type=application/pdf'

The error I get each time:

"{"validation_errors":[{"field":"scan_type","errors":["This value should not be blank."]},{"field":"file","errors":["This value should not be blank."]}],"message":"Input data validation has failed. Please check 'validation_errors' field to get more data."}"

Main code: (The headers are correct for sure)

IHybridAnalysisAPIClient hybrid_analysis_client = BuilderAndServiceHybridAnalysis.GetHybridAnalysisAPIClient();


            // Request Headers 2
            var hybrid_analysis_upload_file_api_headers = new Dictionary<string, string>
            {
                {"api-key", "p36a6qk6119e21dfxf2repsy918df0b2mt5cifpra922df944eg2iim292b1f961"},
                {"Accept", "application/json"},
                {"User-Agent", "Mozilla 5.0"},
                {"Content-Type", "multipart/form-data"}
            };
            byte[] readText = File.ReadAllBytes(@"C:\Users\ariels\Downloads\Ziv.pdf");

            ByteArrayPart file_bytes = new ByteArrayPart(readText, @"C:\Users\ariels\Downloads\Ziv.pdf");

            var response = await hybrid_analysis_client.UploadFile(hybrid_analysis_upload_file_api_headers, "all", file_bytes);

Interface:

        [Multipart]
        [Post("/api/v2/quick-scan/file")]
        Task<Root4> UploadFile([HeaderCollection] IDictionary<string, string> headers,
                                    [AliasAs("scan_type")] string scan_type,
                                    [AliasAs("file")] IEnumerable<ByteArrayPart> file);

Feel free to use the API key, it was created for U guys.


Solution

  • Looks like Refit doesn't do that. Just use the following

    // Not Refit
    using (var httpClient = new HttpClient())
    {
        using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://www.hybrid-analysis.com/api/v2/quick-scan/file"))
        {
            request.Headers.TryAddWithoutValidation("accept", "application/json");
            request.Headers.TryAddWithoutValidation("user-agent", "Mozilla/5.0");
            request.Headers.TryAddWithoutValidation("api-key", "XXX");
    
            var multipartContent = new MultipartFormDataContent();
            multipartContent.Add(new StringContent("all"), "scan_type");
            var file1 = new ByteArrayContent(File.ReadAllBytes(@"C:\Users\ariels\Downloads\Ziv.pdf"));
            file1.Headers.Add("Content-Type", "application/pdf");
            multipartContent.Add(file1, "file", Path.GetFileName("Ziv.pdf"));
            multipartContent.Add(new StringContent("true"), "no_share_third_party");
            multipartContent.Add(new StringContent("false"), "allow_community_access");
            multipartContent.Add(new StringContent("Test"), "comment");
            multipartContent.Add(new StringContent("Test"), "submit_name");
            request.Content = multipartContent;
    
            var response = await httpClient.SendAsync(request);
            //Console.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented));
        }
    }