アラートを作成するAPI呼び出しを作成しようとしていますが、正しく行う方法がわかりません。それは私には分かりませんので、以下のコードを私が書いたものを提供しましたが、なぜBad Requestエラーを返しているのか分かりません。OpsGenieにPOST要求を行うには?
私の要求が間違って書式設定されていたり、何かをしている可能性がありますが、以前はCURLとは何もしていないので、私には分かりません。
それに似ていますカールリクエスト投稿することになっています:
curl -XPOST 'https://api.opsgenie.com/v1/json/alert' -d '
{
"apiKey": "eb243592-faa2-4ba2-a551q-1afdf565c889",
"message" : "WebServer3 is down",
"teams" : ["operations", "developers"]
}'
しかし、それは動作しませんが。関数へ
コール:終わり
OpsGenie.createAlert("1b9ccd31-966a-47be-92f2-ea589afbca8e", "Testing", null, null, new string[] { "ops_team" }, null, null);
、それが不正な要求を返します。どのようにデータや何かを入れているので何か問題があるかどうかわからないので、どんな助けでも大歓迎です。
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);
}
}
}