2016-04-29 189 views
2

我只是面對這個事實,我想創建一個頁面在一定的空間作爲一個定義的父母的孩子。Confluence Rest API:我可以通過空間和父級創建頁面嗎?

有沒有可能通過這些方式來做到這一點? 我還沒找到它。

謝謝! :)

+0

的可能的複製[如何使用他們的REST API來創建新的Confluence的頁面?(http://stackoverflow.com/questions/23523705/how-to-create - 新的頁面,在匯合 - 使用 - 其-REST的API) –

回答

0

Create a new page as a child of another page

這是一個REST API的相似。由於REST API基於開放標準,因此您可以使用任何Web開發語言來訪問API。

在C#中,你可以使用下列選項之一:

  • 的HttpWebRequest/HttpWebResponse
  • WebClient的
  • 的HttpClient(可從.NET 4.5)

我會強烈建議使用HttpClient類,因爲它的設計要比前兩個更好(從可用性的角度來看)。

0

所有你需要的部分:

的createPage功能
的HTTP POST功能
啞類,使樣品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'}}}"; 
相關問題