0

I am testing sending some data to an api. The demo code on their site is copied below (the address and data I have changed as its a clients data).

On the line, using (var streamWritier...) I get the error below,

Cannot send a content-body with this verb-type

I have not seen data sent this way before. Is there something I am missing?

using System;
using System.IO;
using System.Net;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            var authStr = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(@"username:password"));

        var httpRequest = (HttpWebRequest)HttpWebRequest.Create("https://some-api");

        httpRequest.Headers.Add("Authorization", "Basic " + authStr);
        WebResponse response = null;

        try
        {
            httpRequest.ContentType = "application/json";
            httpRequest.Accept = "application/json";
            string data = "{  \"RequestType\": \"ClientAdhoc\",  \"IdentifierType\": \"KDF\",  \"ReqDate\": 20190101"}";
            using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
            { streamWriter.Write(data); streamWriter.Flush(); streamWriter.Close(); }

            response = httpRequest.GetResponse();
            var responseStream = response.GetResponseStream();
            var reader = new StreamReader(responseStream);
            Console.WriteLine("Response received: [{0}].", reader.ReadToEnd());
        }
        catch (Exception exception)
        {
            Console.WriteLine("Exception occurred: [{0}].", exception.Message);
            Console.WriteLine(exception.StackTrace);
        }
       }
     }
    }
mHelpMe
  • 6,336
  • 24
  • 75
  • 150
  • 2
    maybe You need to set HttpMethod to POST – Ramin Rahimzada Oct 02 '19 at 08:27
  • Obvious question: Are you using the correct verb/method? This doesn't look like it should be a GET request, but you're not setting a different method. – ProgrammingLlama Oct 02 '19 at 08:28
  • @John I'm very knowledgeable in this area, I was told this code will work with no need for changes. I am trying to send some data across to the client and then receive a response. So I believe it should be POST – mHelpMe Oct 02 '19 at 08:35
  • @RaminRahimzada thanks. I have just added that and I get a new error, the request was aborted: Could not create SSL/TLS secure channel. So think thats progress! – mHelpMe Oct 02 '19 at 08:36
  • Great, maybe [this](https://stackoverflow.com/questions/2859790/the-request-was-aborted-could-not-create-ssl-tls-secure-channel) also help – Ramin Rahimzada Oct 02 '19 at 08:37
  • _"So I believe it should be POST"_ - but you're not setting POST, and the default on HttpWebRequest is GET. – ProgrammingLlama Oct 02 '19 at 08:40

0 Answers0