C# call REST client with x-api-key within grasshopper

Possibly https://github.com/tonerdo/dotnet-env
Why would you ever include a private key in publicly distributed GHA though?

I wouldn’t. But I do need to include it in a private GHA distributed at the enterprise level, and my concern is that any of our users could just peek in at any time and grab the API key. Do I think that’s going to happen? No, but if somebody wanted to, they could at this point. So I’m trying to understand if there is a best practice for safeguarding that information.

I will look into using Environment Variables, but I’m not sure this solves the problem, since the user can track down the environment variable entry from the decompiled GHA and simply read the variable to get the key. Unless there is some way to encrypt it?

Cheers,
Marc

Thanks for this thread!

In case anyone comes across this. I just gave the code by Luis Fraguada a go and got an error:

It couldn’t find the Streamwriter assembly.

I added “System.IO” before Streamwriter and Streamreader and then it worked just fine to trigger a Webhook in Integromat:

private void RunScript(string x, bool y, ref object A)
  {
    var request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create("https://hook.integromat.com/XXXXXXXXXXXXXXX");
    request.Timeout = 1000;
    request.Method = "POST";
    request.ContentType = "application/json";
    request.Accept = "application/json";

    if (y) {

      using(var streamWriter = new System.IO.StreamWriter(request.GetRequestStream()))
      {
        string json = x;
        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();
      }

    }

    try
    {
      var httpResponse = (System.Net.HttpWebResponse) request.GetResponse();
      using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
      {
        var result = streamReader.ReadToEnd();
        Print(result);
        A = result;
      }
    }
    catch (System.Net.WebException e)
    {
      Print(e.Message);
    }

  }