0

I have a Post call that takes a string GUID "AA0DB615-D4CB-4466-BC23-0E0083002220" I am using HTTPWebRequest to send request but I am not sure how to add this along with my Post request. Basically I have not found any method inside the HTTPWebRequest to send a Post that is just a string or a character datatype. Is their anything like request.AddBody.

I have also looked at the GetResponseStream. Can I use this to write to the body as a string or character data type and send the call. I am stuck on this any help would be great

ABID KHAN
  • 35
  • 6
  • have a look here a question like yours answered: [https://stackoverflow.com/questions/56253696/how-to-pass-header-and-body-value-using-httpwebrequest-in-c](https://stackoverflow.com/questions/56253696/how-to-pass-header-and-body-value-using-httpwebrequest-in-c) – Mohammad Apr 02 '20 at 21:36
  • This is sending the body as a Json. This part I have already done. But how to send as a string when doing a post call – ABID KHAN Apr 02 '20 at 21:46
  • I dont think so . you can send string when you choose myHttpWebRequest.ContentType = "application/x-www-form-urlencoded". see here: https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.contenttype?view=netframework-4.8 – Mohammad Apr 02 '20 at 21:59

1 Answers1

0

Here's a sample request that sends a plain text GUID in the body using HttpWebRequest, You can set the Content type to "text/plain" if you want to explicitly state that the content type is plain text :

var request = (HttpWebRequest)WebRequest.Create("URL GOES HERE");
request.Method = "POST";
var content = Guid.NewGuid().ToString(); //You should replace this with your GUID
var encoding = new ASCIIEncoding();
var bytes = encoding.GetBytes(content);
request.ContentType = "text/plain";
request.ContentLength = bytes.Length;
using (var requestStream = request.GetRequestStream())
{
    requestStream.Write(bytes, 0, bytes.Length);
    var response = request.GetResponse() as HttpWebResponse;
}