【问题标题】:C# - Is it possible to get HTML code from Confluence page via REST API?C# - 是否可以通过 REST API 从 Confluence 页面获取 HTML 代码?
【发布时间】:2021-02-27 18:06:19
【问题描述】:

到目前为止,我已经编写了一个 Azure Function GET 请求,以从 Confluence 页面获取内容及其 ID。
我使用 Postman 向我展示通过 GET 请求获得的页面中的正文(见下文)。
但是,我想从页面中获取的是其中内容的 HTML 代码。

有什么方法可以实现吗?如果有,怎么做?

代码(Visual Studio 2019):

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace LocalFunctionProj
{
    
    public static class Http
    {
        private static HttpClient httpClient = new HttpClient();
        [FunctionName("Http")]
        public static async Task<IActionResult> getContentByID(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
              ILogger log, ExecutionContext context)
        {
            //Set up Configuration Builder
            var confBuild = new ConfigurationBuilder()
                .SetBasePath(context.FunctionAppDirectory)
                .AddJsonFile("local.settings.json")
                .AddEnvironmentVariables()
                .Build();

            //Basic Authentication
            var user = confBuild["ConfluenceUser"];
            var api = confBuild["ConfluenceAPI"];
            var domain = confBuild["ConfluenceDomain"];
              httpClient.DefaultRequestHeaders.Authorization= new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", user, api))));

            object  body = null;

            //Get content from page
            using (HttpResponseMessage response = await httpClient.GetAsync(
                        $"https://{domain}/wiki/rest/api/content/1234"))
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
               
                body = JsonConvert.DeserializeObject(responseBody);
            }
            return new OkObjectResult(body);
        }
    }
}

邮递员输出 (JSON):

{
    "id": "1234",
    "type": "page",
    "status": "current",
    "title": "Test",
    "space": {
        "id": 12345,
        "key": "Test",
        "name": "TEST - title",
        "type": "global",
        "status": "current",
        "_expandable": {
            "settings": "/rest/api/space/Test/settings",
            "metadata": "",
            "operations": "",
            "lookAndFeel": "/rest/api/settings/lookandfeel?spaceKey=Test",
            "identifiers": "",
            "permissions": "",
            "icon": "",
            "description": "",
            "theme": "/rest/api/space/Test/theme",
            "history": "",
            "homepage": "/rest/api/content/123456"
        }, .....

【问题讨论】:

  • 你只能得到 Postman 返回的东西。 HTTP源是服务器上的一个文件,只有访问服务器文件系统才能获取源。

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


【解决方案1】:

要获取 html,您需要通过添加“expand=body.storage”作为查询参数来“扩展”正文部分:

your url -> $"{_baseUrl}/rest/api/content/{Id}?expand=body.storage"

原始 HTML 位于“Page.Body.Storage.value”中

【讨论】:

    猜你喜欢
    • 2015-06-03
    • 2019-01-23
    • 2017-06-06
    • 2011-03-29
    • 1970-01-01
    • 2017-03-10
    • 2017-05-09
    • 2020-12-22
    • 1970-01-01
    相关资源
    最近更新 更多