Craig
Craig

Reputation: 99

convert curl to webrequest in vb.net or c#

I have seen many examples on this site showing the webrequest equivalent of curl, however, as I have absolutely no php experience at all, it still leaves me dumbfounded.

Can somebody please show me the webrequest equivalent of this curl command:

curl  -H "Content-Type:application/json" -X POST -d '{"name":"download_casestudy_a","casestudy":"A","type":"trackLink","href":"http://www.example.com","key":"your_key","session_id":"f33234de-cc75-4f28-9e9a-afb0014a5daf"}' https://in-automate.sendinblue.com/p

Upvotes: 2

Views: 2109

Answers (1)

Kris
Kris

Reputation: 956

Sending a request with your given options and data migth look like this:

static void Main(string[] args)
        {
            var request = WebRequest.Create(new Uri("https://in-automate.sendinblue.com/p"));
            var json =
                "'{'name':'download_casestudy_a','casestudy':'A','type':'trackLink','href':'http://www.example.com','key':'your_key','session_id':'f33234de-cc75-4f28-9e9a-afb0014a5daf'}'";
            request.Headers.Add("Content-Type", "application/json");
            request.Method = "POST";
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }


            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(stream: httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }

        }

Upvotes: 3

Related Questions