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

**URL:** <https://community.postman.com/t/error-when-adding-file-to-restsharp-request-using-postman-c-code/16345>\
**Category:** Help Hub\
**Created:** [September 30, 2020, 6:33am UTC](https://community.postman.com/t/error-when-adding-file-to-restsharp-request-using-postman-c-code/16345 "2020-09-30T06:33:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimonKravis](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@SimonKravis](https://community.postman.com/u/SimonKravis)\
**Post date:** [September 30, 2020, 6:33am UTC](https://community.postman.com/t/error-when-adding-file-to-restsharp-request-using-postman-c-code/16345/1 "2020-09-30T06:33:23Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 30, 2020, 11:38am UTC](https://community.postman.com/t/error-when-adding-file-to-restsharp-request-using-postman-c-code/16345/2 "2020-09-30T11:38:55Z")

</div>

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

> **[RestRequest.AddFile, RestSharp C# (CSharp) Code Examples - HotExamples](https://csharp.hotexamples.com/examples/RestSharp/RestRequest/AddFile/php-restrequest-addfile-method-examples.html)**
>
> C# (CSharp) RestSharp RestRequest.AddFile - 34 examples found. These are the top rated real world C# (CSharp) examples of RestSharp.RestRequest.AddFile extracted from open source projects. You can rate examples to help us improve the quality of...

You might want to submit an issue on github for that

---

<div class="post-metadata">

**Author:** ![SimonKravis](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@SimonKravis](https://community.postman.com/u/SimonKravis)\
**Post date:** [October 1, 2020, 12:08am UTC](https://community.postman.com/t/error-when-adding-file-to-restsharp-request-using-postman-c-code/16345/3 "2020-10-01T00:08:20Z")

</div>

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](https://github.com/kairosinc/api-examples)

---

<div class="post-metadata">

**Author:** ![bongleebong](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@bongleebong](https://community.postman.com/u/bongleebong)\
**Post date:** [September 8, 2021, 12:36pm UTC](https://community.postman.com/t/error-when-adding-file-to-restsharp-request-using-postman-c-code/16345/4 "2021-09-08T12:36:56Z")

</div>

Hi all,

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

---

<div class="post-metadata">

**Author:** ![romanmueller](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@romanmueller](https://community.postman.com/u/romanmueller)\
**Post date:** [May 2, 2022, 7:24am UTC](https://community.postman.com/t/error-when-adding-file-to-restsharp-request-using-postman-c-code/16345/5 "2022-05-02T07:24:20Z")

</div>

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:

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

```
