ajtatum ajtatum - 3 months ago 23
C# Question

How to convert this HipChat curl post to C#?

I'm trying to create a notification for HipChat and unfortunately their HipChat-CS (https://github.com/KyleGobel/Hipchat-CS) nuget package doesn't work. So, I wanted to investigate how I could manually send a notification but their example is using cURL, which I'm unfamiliar with. I'd like to take their example and make it into something I can use with my c# application. Any help would be greatly appreciated! Here's the cURL example:

curl -d '{"color":"green","message":"My first notification (yey)","notify":false,"message_format":"text"}' -H 'Content-Type: application/json' https://organization.hipchat.com/v2/room/2388080/notification?auth_token=authKeyHere


Thank you again!

AJ

Answer

You can use an HttpWebRequest to establish the connection and send your json payload. You'll need to add System.Net to your using directives.

string jsonContent = "{\"color\":\"green\",\"message\":\"My first notification (yey)\",\"notify\":false,\"message_format\":\"text\"}";
byte[] jsonBytes = Encoding.ASCII.GetBytes(jsonContent);
var request = (HttpWebRequest) WebRequest.Create("https://organization.hipchat.com/v2/room/2388080/notification?auth_token=authKeyHere");
request.ContentType = "application/json";
request.Method = "POST";
request.ContentLength = jsonBytes.Length;

using (var reqStream = request.GetRequestStream())
{
    reqStream.Write(jsonBytes, 0, jsonBytes.Length);
    reqStream.Close();
}

 WebResponse response = request.GetResponse();
//access fields of response in order to get the response data you're looking for.