@martensit

Где может быть ошибка в моем сниппете POST запроса на c#?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Text;
using System.IO;

namespace POST
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new HttpClient())
            {
                var url = "https://dns.api.gandi.net/api/v5/zones";
                var data = new StringBuilder();
                data.AppendLine(@"{""name"": ""megaso55.pw Zone""}");
                using (var content = new StringContent(data.ToString(), Encoding.UTF8, "text/plain"))
                {
                    using (var request = new HttpRequestMessage(HttpMethod.Put, url))
                    {
                        request.Headers.Add("X-Api-Key", "osuheropbwouibheruibwpj");
                        request.Content = content;
                        using (var response = client.SendAsync(request).Result)
                        {
                            var result = response.Content.ReadAsStringAsync().Result;
                            Console.WriteLine(result);
                            File.WriteAllText(@"C:\result.txt", result);
                        }
                    }
                }
                System.Threading.Thread.Sleep(1000000);
            }
        }
    }
}


Получаю
{"code": 405, "message": "The server could not comply with the request since it is either malformed or otherwise incorrect.", "object": "HTTPMethodNotAllowed", "cause": "Method Not Allowed"}


Через CURL этот запрос должен так отправляться
$ curl -D- -X POST -H "Content-Type: application/json" \
             -H "X-Api-Key: $APIKEY" \
             -d '{"name": "example.com Zone"}' \
             https://dns.api.gandi.net/api/v5/zones

Так в документации сказано.
Где ошибка может быть в моем сниппете?
  • Вопрос задан
  • 103 просмотра
Решения вопроса 1
using (var request = new HttpRequestMessage(HttpMethod.Put, url))

HttpMethod.Post
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы