【问题标题】:Confluence Rest API: Can I create a page by space and parent?Confluence Rest API:我可以按空间和父级创建页面吗?
【发布时间】:2016-04-29 07:38:48
【问题描述】:

我只是面对这样一个事实,即我想在某个空间中创建一个页面作为已定义父级的子级。

有没有可能通过这些方式做到这一点? 我还没找到。

谢谢! :)

【问题讨论】:

标签: c# rest confluence confluence-rest-api


【解决方案1】:

请查看文档: Create a new page as a child of another page

这是 curl,但在 C# 中应该是类似的

【讨论】:

    【解决方案2】:

    Create a new page as a child of another page

    这是一个 REST API。由于 REST API 基于开放标准,因此您可以使用任何 Web 开发语言来访问 API。

    在 C# 中,您可以使用以下选项之一:

    • HttpWebRequest / HttpWebResponse
    • 网络客户端
    • HttpClient(从 .NET 4.5 开始可用)

    我强烈推荐使用 HttpClient 类,因为它的设计(从可用性的角度来看)比前两者要好得多。

    【讨论】:

      【解决方案3】:

      您需要的所有部件:

      createPage 函数
      http 发帖功能
      使示例 json 合格的哑类。
      示例 json 和基本 url

      internal static string createAPage(string space, string title, string body, string objectType, int ancestorId = -1)
              {
                  string json = string.Empty;
                  try
                  {
      
                      CreateContentWithParentJson createContentWithParentJson = JsonConvert.DeserializeObject<CreateContentWithParentJson>(_jsonCreatePageWithParentSample);
                      createContentWithParentJson.space.key = space;
                      createContentWithParentJson.title = title;
                      body = body.Replace("&", "&amp;");
                      createContentWithParentJson.body.storage.value = "<p>" + body + "</p>";
                      Ancestor a = new Ancestor();
                      a.id = ancestorId;
                      createContentWithParentJson.ancestors = new List<Ancestor>();
                      createContentWithParentJson.ancestors.Add(a);
                      json = JsonConvert.SerializeObject(createContentWithParentJson, Utils.getJsonSettings());
      
              }
              catch (Exception) {  // handle exception 
              }
              return Utils.contentPost(json, _baseUrl);
      
          }
      
      
        internal static string contentPost(string postData, string url, string method = "POST")
              {
                  string encodedCred = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(ConfAPI._confUser + ":" + ConfAPI._confPass));
                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                  request.Headers.Add("Authorization", "Basic " + encodedCred);
                  request.Headers.Add("X-Atlassian-Token", "no-check");
                  request.Method = method;
                  request.Accept = "application/json";
                  request.ContentType = "application/json";
      
                  // request.KeepAlive = false;
                  if (postData != null)
                  {
                      byte[] data = Encoding.UTF8.GetBytes(postData);
                      request.ContentLength = data.Length;
                      using (var stream = request.GetRequestStream())
                      {
                          stream.Write(data, 0, data.Length);
                      }
                  }
                  WebResponse response;
      
                  try
                  {
                      response = (HttpWebResponse)request.GetResponse();
                  }
                  catch (WebException e)
                  {
                      return e.Message + " " + e.StackTrace.ToString();
                  }
      
                  var responsestring = new StreamReader(response.GetResponseStream()).ReadToEnd();
                  return responsestring;
              }
      
        public class CreateContentWithParentJson
          {
              public string type { get; set; }
              public string title { get; set; }
              public List<Ancestor> ancestors { get; set; }
              public Space space { get; set; }
              public Body body { get; set; }
          }
      
       internal static string _baseUrl = "http://localhost:8090/rest/api/content";
       internal static string _jsonCreatePageWithParentSample = @"{'type':'page','title':'new page', 'ancestors':[{'id':456}], 'space':{'key':'TST'},'body':{'storage':{'value':'<p>This is a new page</p>','representation':'storage'}}}";
      

      【讨论】:

        猜你喜欢
        • 2015-10-15
        • 1970-01-01
        • 2017-03-10
        • 1970-01-01
        • 2017-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多