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?
 
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
 
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
 
Mike MillerSoftware EngineerAuthor Commented:
This is perfect. Got it. Thanks so much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.