首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何向OpsGenie发出POST请求?

如何向OpsGenie发出POST请求?
EN

Stack Overflow用户
提问于 2016-07-08 13:27:29
回答 0查看 1.4K关注 0票数 2

我正在尝试使用API调用来创建警报,但我不确定如何正确完成此操作。这对我来说没有多大意义,所以我提供了下面我写的代码,但我不明白为什么它会返回一个Bad Request错误。

可能是我的请求格式不正确,或者其他什么,但我不知道,因为我以前从来没有做过与CURL有关的事情。

它应该发布一个curl请求,看起来类似于:

代码语言:javascript
复制
curl -XPOST 'https://api.opsgenie.com/v1/json/alert' -d '
{
     "apiKey": "eb243592-faa2-4ba2-a551q-1afdf565c889",
      "message" : "WebServer3 is down",
      "teams" : ["operations", "developers"]
 }'

但它不起作用。

对函数的调用:

代码语言:javascript
复制
OpsGenie.createAlert("1b9ccd31-966a-47be-92f2-ea589afbca8e", "Testing", null, null, new string[] { "ops_team" }, null, null);

最后,它返回一个错误的请求。我不知道我输入数据或其他东西的方式是否有问题,因此如果有任何帮助,将不胜感激。

代码语言:javascript
复制
public static void createAlert(string api, string message, string description, string entity, string[] teams, string user, string[] tags)
    {
        var request = WebRequest.Create(new Uri("https://api.opsgenie.com/v1/json/alert"));
        string json = "{";
        if (api != null)
            json = json + "'apiKey': '" + api + "'";
        if (message != null)
            json = json + ", 'message': '" + message + "'";
        if (description != null)
            json = json + ", 'description': '" + description + "'";
        if (entity != null)
            json = json + ", 'entity': '" + entity + "'";
        if (teams != null)
        {
            json = json + ", 'teams': '['" + string.Join(",", teams) + "']'";
        }
        if (user != null)
            json = json + ", 'user': '" + user + "'";
        if (tags != null)
            json = json + ", 'tags': '" + tags.ToString() + "'";
        json = json + "}";
        Console.WriteLine(json);
        request.Method = "POST";
        try
        {
            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();
                dynamic obj = JsonConvert.DeserializeObject(result);
                var messageFromServer = obj.error.message;
                Console.WriteLine(messageFromServer);

            }
        }
        catch (WebException e)
        {
            if (e.Status == WebExceptionStatus.ProtocolError)
            {
                Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
            }
            else
            {
                Console.WriteLine(e.Message);
            }

        }
    }
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38259441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档