Getting this error message "Cannot send a content-body with this verb-type."

I'm encountering this message upon WebClient request to the API in C# using the GET method, did I miss something on my code? but when I use the POST method it shows the same output when I test it on PostMan which is the error because POST is not the method to use in this link

Here is my code.

 [WebMethod]
public string HelloWorld()
{
    string module = "/heartbeat";
    string method = "GET";
    string link = sandboxURL;

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // 3072 is SecurityProtocol.Tls12
    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

    var response = "";
    string body = "";
    var _link = link + module;
    using (var client = new WebClient())
    {
        try
        {
            client.Headers.Add("Content-Type:application/json");
            client.Headers.Add("Authorization", "Bearer "+ JWTToken);

            //var url = new Uri(_link);

            response = client.UploadString(_link, method, body);
        }
        catch(WebException ex)
        {
            if (ex.Status == WebExceptionStatus.ProtocolError)
            {
                HttpWebResponse wrsp = (HttpWebResponse)ex.Response;
                var statusCode = (int)wrsp.StatusCode;
                var msg = wrsp.StatusDescription;

                Stream stream = wrsp.GetResponseStream();

                byte[] data = new byte[4096];
                int read;
                StringBuilder sb = new StringBuilder();
                while ((read = stream.Read(data, 0, data.Length)) > 0)
                {
                    sb.Append(ASCIIEncoding.ASCII.GetString(data));
                }
            }
        }
    }

        return response;
}

Upvotes: 0

Views: 446

Answers (0)

Related Questions