Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API - Upload File error via C# #539

Open
camaralima opened this issue Dec 7, 2024 · 0 comments
Open

REST API - Upload File error via C# #539

camaralima opened this issue Dec 7, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@camaralima
Copy link

Describe the bug
First of all, the example provided in the File Upload documentation is incorrect, such as adding the Bearer token twice, and the File parameter is set as text. The test via the web page also always results in an error.
Using Postman, the web interface, and Python with HTTP Client, I am able to successfully upload the file and later perform the Get request without any issues. However, when it comes to C# .Net, it doesn't work. The example and simple Upload implementation just return an error.
Could you provide a simple File Upload example?

Files
02593-3_HUMBERG AGRIBRA_11-2024_1_Individual.pdf

Job ID
I don't have a Job ID because it was not generated in any of the tests via C#.
This is the ID using Python HTTP Client db95fba2-5ee0-4fff-95b6-708256ced7c9, and this is the ID using the library in Python 5b5f13bc-31d3-4420-af65-130652bdcacf.

Client:
Please remove untested options:

  • API

Additional context
The error:

BadRequest - {"detail":"There was an error parsing the body"}

Below is the snippet of my code that is throwing an error:

public static async Task UploadFileAsync(byte[] byteArrayFile, FileStream fileStream, string fileName)
{
    string url = _baseUrl + _baseUrlUpload; 

    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey);

        using (var content = new MultipartFormDataContent())
        {
            var fileContent = new ByteArrayContent(byteArrayFile);
            //var fileContent = new StreamContent(fileStream); 

            content.Add(fileContent, "file", fileName);
            content.Add(new StringContent("true"), "fast_mode");
            content.Add(new StringContent("en"), "language"); 

            try
            {
                HttpResponseMessage response = await client.PostAsync(url, content); 

                string responseContent = await response.Content.ReadAsStringAsync(); 

                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine(responseContent);
                }
                else
                {
                    Console.WriteLine($"Fail to send file: {response.StatusCode} - {responseContent}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}

Or when I make any changes that return an ID, I get the following error:

{
    "id": "a2a0480d-0a9e-4c28-b057-18dcfb3fe46d",
    "status": "ERROR",
    "error_code": "_UNKOWN_ERROR",
    "error_message": "UNKNOWN_ERROR: COULD_NOT_LOAD_FILE"
}
@camaralima camaralima added the bug Something isn't working label Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant