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