M
M
martensit2018-07-10 23:31:50
C++ / C#
martensit, 2018-07-10 23:31:50

Where could be the error in my c# POST request snippet?

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);
            }
        }
    }
}

I receive
{"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"}

Through CURL this request should be sent like this
$ 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

That's what the documentation says.
Where is the error in my snippet?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-07-10
@martensit

using (var request = new HttpRequestMessage(HttpMethod.Put, url))

httpMethod. Post

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question