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

Mike MillerSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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().
0
ste5anSenior DeveloperCommented:
Please rephrase your question. What do you like to do really?
0
Mike MillerSoftware EngineerAuthor Commented:
I'm not sure I know how to rephrase it? I want to run that command from an application developed in C#.
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Mike MillerSoftware EngineerAuthor Commented:
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?
0
ste5anSenior DeveloperCommented:
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?
1
Flabio GatesCommented:
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

0
Flabio GatesCommented:
(Based on sample code on MSDN.)
Untested but this should get you started on using WebClient:
namespace ConsoleApplication1
{
    using System;
    using System.Collections.Specialized;
    using System.Net;
    using System.Text;

    class Program
    {
        static void Main(string[] args)
        {
            // get user_name and password from your Authorization: Basic aW1fYV...NfbXk= header
            var client = new WebClient { Credentials = new NetworkCredential("user_name", "password") };
            var data = new NameValueCollection();
            data.Add("key_attribute", "email");
            data.Add("key_value", "'\"test@example.com\"'");
            data.Add("value", "'{\"externalID\":\"123456\"}'");

            var resp = client.UploadValues("https://my-app.blahblah.com/entity.update", data);
            Console.WriteLine("\nResponse received was :\n{0}", Encoding.ASCII.GetString(resp));
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike MillerSoftware EngineerAuthor Commented:
This is perfect. Got it. Thanks so much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.