Invoke code C# error

Console.WriteLine(ImgFile);
var client = new RestClient(“http://125.227.53.125:10082/file-upload”);
client.Timeout = 60000;
var request = new RestRequest(Method.POST);
request.AddHeader(“Content-Type”,“multipart/form-data”);
request.AddHeader(“file”,ImgFile);

IRestResponse response = client.Execute(request);
Console.WriteLine(response.content);
code = response.content;

I put above code into invoke code activity then got the error below.
image
How to solve the error…?
Please help…Thanks advance!!


this is arguments.


I had read this.

Hi,

Can you try the following steps?

  1. Install UiPath.Web.Activites Package using Package Manager on ribbon menu of Studio.
  2. Add “RestSharp” namespace in “Imports” tab.
  3. Then, you might need to restart Studio and reopen your project.
  4. Modify your code at line 9 and 10 as the following.

Console.WriteLine(response.Content);
code = response.Content;

(Because C# is case sensitive)

Regards,

It works. Thanks a lot.
I also try in vb.net code.
Dim client As var = New RestClient(“http://*****”)
Dim request As var = New RestRequest(Method.POST)
request.AddHeader(“Postman-Token”, “359aa3af-392a-4790-8e9a-25231496ceb5”)
request.AddHeader(“cache-control”, “no-cache”)
request.AddHeader(“content-type”, “multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW”)
request.AddParameter(“multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW”, “------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=”,doc_type"\r\n\r\numc_invoice\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=",file_type"\r\n\r\nimg\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=",img_file"; filename=“C:\Users\admin\Desktop\API\0001.jpg”\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW–", ParameterType.RequestBody)

Dim response As IRestResponse = client.Execute(request)

result = response

Hi,

Which language do you use, VB or C#?
In previous post, your code seems C# but above error is from VB.

Can you check the following sample?

Sample20200814-1.zip (14.2 KB)

Regards,

sorry I also tried VB.net code

image

It looks like this topic was heavily auto-marked as spam :roll_eyes: , I restored all the posts of you @chienlin.

Hi,

var is not variable type in vb.net. You need to write proper variable type such as RestClient.
If you want to write single line over multiple lines, you need to add space + _ at the end of line.

The following is vb.net version of my previous post. Hope it helps you.

Console.WriteLine(ImgFile)
Dim client As RestClient = New RestClient(“http://125.227.53.125:10082/file-upload”)
client.Timeout = 60000
Dim request As RestRequest = New RestRequest(Method.POST)
request.AddHeader(“Content-Type”,“multipart/form-data”)
request.AddHeader(“file”,ImgFile)

Dim response As IRestResponse = client.Execute(request)
Console.WriteLine(response.Content)
code = response.Content

Regards,

It’s really really help!!! thanks a lot~~
but the result is not what I want…
it’s empty :sweat_smile:
it’s also the same result that I use http request activity
image

How can I get the result that I attach below?

Hi,

Perhaps you should check content.StatusCode etc because it seems there is some error.
In Invoke code activity, it’s not very easy to debug code. If possible, perhaps you should use not Invoke code activity but Assign activity and Invoke Method activity in order to debug easier.

Regards,

That worked for me… finally!