Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ishibashi.futos committed Dec 16, 2018
1 parent cb70500 commit 0c8b738
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions WordpressPostApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static void Main(string[] args)
post.SetTags(tags, req);

// エラーが無ければPost投稿を行う
var postResult = Regex.Unescape(req.Post("/wp-json/wp/v2/posts", post));
var postResult = Regex.Unescape(req.Post("/wp-json/wp/v2/posts", post.ToJson()));
Console.WriteLine(string.Format("投稿に成功しました:{0}", postResult));
}
catch (Exception e)
Expand Down Expand Up @@ -129,6 +129,15 @@ public void SetTags(List<string> slugs, HttpRequest req)
tags = result;
}
public string status;
/// <summary>
/// 自分自身をJSONに変換するメソッド
/// </summary>
/// <returns>string</returns>
public string ToJson()
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(this);
}
}

/// <summary>
Expand Down Expand Up @@ -201,10 +210,12 @@ public string Get(string url)
/// <summary>
/// Postリクエストを実行するメソッド。
/// </summary>
public string Post(string url, PostContent data)
/// <param name="url">requestUrl</param>
/// <param name="json">PostData(json)</param>
/// <returns>string</returns>
public string Post(string url, string json)
{
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
// Content-Type: application/jsonとしてセット
var content = new StringContent(json, new UTF8Encoding(), "application/json");
HttpResponseMessage res = client.PostAsync(Uri.EscapeUriString(baseUrl + url), content).Result;
Expand Down

0 comments on commit 0c8b738

Please sign in to comment.