Link to home
Start Free TrialLog in
Avatar of Member_2_1242703
Member_2_1242703

asked on

Executing CURL statement in C#

How would I execute this statement in a C# windows application?

curl -X POST \
    -H "Authorization: Basic aW1fYV...NfbXk=" \
    --data-urlencode key_attribute=email \
    --data-urlencode key_value='"test@example.com"' \
    --data-urlencode value='{"externalID":"123456"}' \
    https://my-app.blahblah.com/entity.update

Open in new window

Avatar of David Favor
David Favor
Flag of United States of America image

Set a variable to the above command + then call system($command)... or...

You can refer to the CURL site for details about how to do all this programmatically.

Unless you deeply understand using libcurl primitives, best to stick with executing the curl command via system().
Please rephrase your question. What do you like to do really?
Avatar of Member_2_1242703
Member_2_1242703

ASKER

I'm not sure I know how to rephrase it? I want to run that command from an application developed in C#.
So like this...

            var sb = new StringBuilder();
            sb.Append("curl -X POST \\");
            sb.Append("-H \"Authorization:Signature myAuthStringHere=\" \\");
            sb.Append("--data-urlencode key_attribute=email \\");
            sb.Append("--data-urlencode key_value='" + email + "' \\");
            sb.Append("--data-urlencode value='{\"externalID\":\" " + externalid + " \"}' \\");
            sb.Append("https://my-app.blahblah.com/entity.update");
            //System.Diagnostics.Process.Start("CMD.exe", sb.ToString());

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = sb.ToString();
            process.StartInfo = startInfo;
            process.Start();;

Open in new window


??
Not working for me. Am I approaching this the correct way?
Well, it looks like you want to call a web service. The question is, why relying on an external program and doing the request using WebClient?
As requested but you should also look into using WebClient as ste5an says:
namespace ConsoleApplication1
{
    using System.Diagnostics;
    using System.Text;

    class Program
    {
        static void Main(string[] args)
        {
            var sb = new StringBuilder();
            sb.Append(" -H \"Authorization: Basic aW1fYV...NfbXk=\"");
            sb.Append(" --data-urlencode key_attribute=email");
            sb.Append(" --data-urlencode key_value='\"test@example.com\"'");
            sb.Append(" --data-urlencode value='{\"externalID\":\"123456\"}'");
            sb.Append(" https://my-app.blahblah.com/entity.update");

            //sb.Append(" -o google.html");
            //sb.Append(" https://google.com");

            var psi = new ProcessStartInfo("curl.exe", sb.ToString());
            var p = new Process();
            p.StartInfo = psi;
            p.Start();
            p.WaitForExit();
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Flabio Gates
Flabio Gates

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is perfect. Got it. Thanks so much!