简体   繁体   中英

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

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.

To use HipChat-CS, I had to manually uninstall the automatically downloaded dependent package ServiceStack, and manually install a specific version, 4.0.56, of ServiceStack. Everything worked once I did this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM