Http запрос проходит в Postman и PowerShell но не проходит в C# httpclient

Сабж.

Вот CURL запрос для импорта в Postman

curl --location 'https://api.telegra.ph/createPage' \
--header 'Content-Type: application/json' \
--data '{"access_token":"26fb54ae0ace42a66d743ec00519a604236dca21e80b588d012833b6955d", "title": "Mytitle", "content": [{"tag": "p", "children": ["Hello, world!"]}]}'

Ответ введите сюда описание изображения Также запрос проходит в PowerShell

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")

$body = @"
{`"access_token`":`"26fb54ae0ace42a66d743ec00519a604236dca21e80b588d012833b6955d`", `"title`": `"Mytitle`", `"content`": [{`"tag`": `"p`", `"children`": [`"Hello, world!`"]}]}
"@

$response = Invoke-RestMethod 'https://api.telegra.ph/createPage' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

Вот C# код https://dotnetfiddle.net/eRJHRN который генерирует POSTMAN,

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.telegra.ph/createPage");
var content = new StringContent("{\"access_token\":\"26fb54ae0ace42a66d743ec00519a604236dca21e80b588d012833b6955d\", \"title\": \"Mytitle\", \"content\": [{\"tag\": \"p\", \"children\": [\"Hello, world!\"]}]}", null, "application/json");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());

в ответ приходит:

{"ok":false,"error":"ACCESS_TOKEN_INVALID"}

Якобы не валидный токен.

Почему так?


Ответы (1 шт):

Автор решения: User12351259599491

Не знаю почему но вот так запрос проходит: https://dotnetfiddle.net/VaZn1s

using (var ClientHandler = new HttpClientHandler())
{
    using (var Client = new HttpClient(ClientHandler))
    {
        Dictionary<string, string> dic = new Dictionary<string, string>()
        {
        ["access_token"] = "26fb54ae0ace42a66d743ec00519a604236dca21e80b588d012833b6955d",
        ["title"] = "Mytitle",
        ["author_name"] = null,
        ["author_url"] = null,
        ["return_content"] = "false",
        ["content"] = "[\"Hello, world!\"]"
        };

        var content = new FormUrlEncodedContent(dic);
        var Response = await Client.PostAsync("https://api.telegra.ph/createPage", content);
        Console.WriteLine(await Response.Content.ReadAsStringAsync());
                             
    }
}
→ Ссылка