[英]C# - Is it possible to get HTML code from Confluence page via REST API?
到目前為止,我已經編寫了一個 Azure 函數 GET 請求,以從帶有其 ID 的 Confluence 頁面獲取內容。
我使用 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"
}, .....
要獲取 html,您需要通過添加 'expand=body.storage' 作為查詢參數來“擴展”正文部分:
your url -> $"{_baseUrl}/rest/api/content/{Id}?expand=body.storage"
原始 HTML 位於“Page.Body.Storage.value”中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.