Smartling / api-sdk-net

.Net SDK for integrating with the Smartling API. The Smartling API allows developers to upload language specific resource files and download the translations of those files for easy integration within their application. http://docs.smartling.com
Apache License 2.0
1 stars 6 forks source link

FileApiClient.UploadFileContents(...) doesn't work under Linux #40

Open ojacobse opened 2 years ago

ojacobse commented 2 years ago

When running the code under .net 6 (not tested other versions) the code fails when running under Linux.

Note: For others in the same situation I solved it by overriding the FileApiClient.UploadFileContents(...) But use the HttpClientwith HttpRequestMessageand MultipartFormDataContent.

Request (Captured by proxy)

POST /files-api/v2/projects/<REMOVED_PROJECT_ID>/file HTTP/1.1
Host: <REMOVED>
User-Agent: smartling-api-sdk-net/2.4.42
Content-Length: 797
Authorization: Bearer <REMOVED>
Content-Type: multipart/form-data; boundary=----------88d87d9940a3423abe7fe6388f893929
Accept-Encoding: gzip

------------88d87d9940a3423abe7fe6388f893929
Content-Disposition: form-data; name="fileUri";

/file/11111111-1111-1111-1111-111111111111
------------88d87d9940a3423abe7fe6388f893929
Content-Disposition: form-data; name="fileType";

json
------------88d87d9940a3423abe7fe6388f893929
Content-Disposition: form-data; name="smartling.client_lib_id";

{"client":"smartling-api-sdk-net","version":"2.4.42"}
------------88d87d9940a3423abe7fe6388f893929
Content-Disposition: form-data; name="file"; filename="/file/11111111-1111-1111-1111-111111111111"
Content-Type: application/octet-stream

{"A":"Lorem","B":"Ipsum"}
------------88d87d9940a3423abe7fe6388f893929--

Response when running the test on Windows

HTTP/1.1 200 OK

{
  "response": {
    "code": "SUCCESS",
    "data": {
      "wordCount": 2,
      "stringCount": 2,
      "overWritten": false
    }
  }
}

Response when running the test on Linux

HTTP/1.1 400 Bad Request

{
    "response": {
        "code": "VALIDATION_ERROR",
        "errors": [
            {
                "key": "file.required",
                "message": "File is required",
                "details": {
                    "field": "file"
                }
            },
            {
                "key": "fileUri.required",
                "message": "fileUri is required",
                "details": {
                    "field": "fileUri"
                }
            },
            {
                "key": "fileType.required",
                "message": "fileType is required",
                "details": {
                    "field": "fileType"
                }
            }
        ]
    }
}
liorMor commented 3 months ago

I'm having the same issue on mac. can you please share the code that you used that worked for you?

liorMor commented 3 months ago

Well... for the next generations, here is is

private async Task UploadFile(string fileUri, string fileContent, string fileType, string projectId, string token)
{
    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://api.smartling.com");
        client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

        // Prepare form data
        var formData = new MultipartFormDataContent();
        formData.Add(new StringContent(fileType), "fileType");
        formData.Add(new StringContent(fileUri), "fileUri");
        formData.Add(new ByteArrayContent(Encoding.UTF8.GetBytes(fileContent)), "file", fileUri);

        // Make the API call
        HttpResponseMessage response = await client.PostAsync($"/files-api/v2/projects/{projectId}/file", formData);

        // Check the response
        if (!response.IsSuccessStatusCode)
        {
            Console.WriteLine($"Failed to upload file. Status code: {response.StatusCode}");
            string errorMessage = await response.Content.ReadAsStringAsync();
            Console.WriteLine($"Error message: {errorMessage}");
        }
    }
}