Error when adding file to RestSharp request using Postman C# code

After successfully using Postman to send a JPEG image to Kairos, I tried using the suggested C# code as shown below from a C# .Net environment:

var client = new RestClient(“https://api.kairos.com/detect”);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader(“Content-Type”, “application/json”);
request.AddHeader(“app_id”, “xxx”);
request.AddHeader(“app_key”, “xxxx”);
request.AddFile(“image”, “/C:/Users/Simon/AppData/Local/Temp/Test (36).jpg”);
IRestResponse response = client.Execute(request);

This caused an illegal path error in Request.Addfile before execution. Removing the first / from the second AddFile parameter, the command was executed but returned the error “image one or more required parameters are missing”. Changing all the / characters to \ did not make any difference.

I have tried converting the file to a byte array as shown below:

 byte [] fileBytes = File.ReadAllBytes(imageFilePath);
            request.AddFileBytes("image", fileBytes, "imageFilePath", "application/octet-stream");

and received the same error

1 Like

To me it looks like the generated code is just wrong (unless that method is overloaded)

You might want to submit an issue on github for that

I agree - I can get the same error in Postman using a 1st (Key) parameter which is not “image” or a 2nd (Value) parameter which is a non-existent file.

I have added a suggestion to add demo code for C# to https://github.com/kairosinc/api-examples

Hi all,

I get the same error too. Do we have a solution for this yet?

I would submit the file as follows, because the component seems to have an error when adding the File with AddFile(“image”, fileName) in some cases when the file name contains any stupid characters:

FileInfo fi = new FileInfo("C:/Users/Simon/AppData/Local/Temp/Test (36).jpg");
request.AddFile(“image”, File.ReadAllBytes(fi.FullName, "Image" + fi.Extension);