Invoke c# code compilation error

Hi @kowts ,

i tried to call a rest api with invoke code in C# ,

it’s a free api for test :

I copied the code and insert them in invoke code activity ,

it give this error
error CS1003: Erreur de syntaxe, ‘(’ attenduc:\Users\DELL\AppData\Local\Temp\mxqntruf.0.cs(22,30) : error CS1026: ) attendue

the copied code :
using System.Net.Http.Headers;
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(“https://text-analysis12.p.rapidapi.com/text-mining/api/v1.1”),
Headers =
{
{ “X-RapidAPI-Key”, “c46259ecc6msh68576a8d2542dadp1205c9jsnb9ae8c2612af” },
{ “X-RapidAPI-Host”, “text-analysis12.p.rapidapi.com” },
},
Content = new MultipartFormDataContent
{
new StringContent(“”)
{
Headers =
{
ContentType = new MediaTypeHeaderValue(“application/octet-stream”),
ContentDisposition = new ContentDispositionHeaderValue(“form-data”)
{
Name = “input_file”,
FileName = “test.txt”,
}
}
},
new StringContent(“english”)
{
Headers =
{
ContentDisposition = new ContentDispositionHeaderValue(“form-data”)
{
Name = “language”,
}
}
},
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}

Invoke code is wrapping a function. you cannot use “import” inside a fuction.

If you type Console.Writeline("Hi mom") inside Invoke code it will generated someting like

using first.import.from.importstab;
using second.import.from.importstab;
namespace SomeNamespace { public class {
    public static variable1type variable1 = default(variable1type);
    public static variable2type variable2 = default(variable2type);
    public static void ExpressionValue() { 
       Console.Writeline("Hi mom")
    }
}}

It compiles this to a type and calls ExpressionValue()
This is why you get debug messages in the console, to let you know the number of lines added to the type, so when you get an error message you know how many lines to subtract from the line number in the error message to find it in your invoke code code editor

Thank you for reply ,

What is the suggestion in this case ,
Do you have an example of C# invoke code running OR is there another solution ?
Your help is much appreciated.

As i wrote, remove the using System.Net.Http.Headers;
and add the fqdn in the code instead ( or add System.Net.Http.Headers in the imports tab )

I added System.Net.Http.Headers in the imports tab ,

But i have this error

Is there an example of invoke code running with C# calling a rest api ?
it will be very helpful for us.

var client = new System.Net.Http.HttpClient();
var request = new System.Net.Http.HttpRequestMessage
{
    Method = System.Net.Http.HttpMethod.Post,
    RequestUri = new Uri("https://text-analysis12.p.rapidapi.com/language-detection/api/v1.1"),
    Headers = {
        { "X-RapidAPI-Key", "SIGN-UP-FOR-KEY" },
        { "X-RapidAPI-Host", "text-analysis12.p.rapidapi.com" },
    },
    Content = new System.Net.Http.StringContent("{    \"text\": \"Hello how are you? Have you heard about SpaceX?\"}")
    {
        Headers = { ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json") }
    }
};

var responseTask = client.SendAsync(request);
responseTask.Wait();
var response = responseTask.Result;
response.EnsureSuccessStatusCode();

var bodyTask = response.Content.ReadAsStringAsync();
bodyTask.Wait();
var body = bodyTask.Result;
Console.WriteLine(body);

1 Like

Thank you ,

it compile.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.