0

I am using Soap API (asmx) as web service for my project and I need to send a POST request with JSON data. I saw a code snippet that shows how to send the POST request using HttpWebRequest. This is the code.

string url = "http://myserver.rocket.com.my/WebService1.asmx/AddCompany";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
     string json = JsonConvert.SerializeObject(company);

     streamWriter.Write(json);
     streamWriter.Flush();
     streamWriter.Close();
}

My problem is, I am not very sure how to handle the request on server side. Can get any example or guide on how to obtain the json content from the stream?

batwing
  • 257
  • 1
  • 8
  • 22
  • yes. But I am not sure how to get the JSON string from the stream as you can see, the code does not sending any parameter with the url. How I can get data that is written to the stream from server side? is it using [FromBody] ? – batwing Aug 06 '18 at 09:34
  • have a look at [this](https://stackoverflow.com/a/4015346/2417602) – vikscool Aug 06 '18 at 09:44
  • @vikscool its all about how to perform the POST request. I need to know how to handle the post request on server side. – batwing Aug 06 '18 at 09:50
  • to handle them you need to create a middleware which is going to act as someone who is going to handle the request, response and errors and would return the output to the caller. – vikscool Aug 06 '18 at 10:22

1 Answers1

1

Well, this is a very big topic but to keep things simple, you could take shelter with one of the existing server-side frameworks.

This also means that you need to select the right kind of programming language. My favourite language is Python but you could very well go with Java, Scala, Ruby, Pearl, PHP, GoLang ... the list is quite big.

If you speak Python then I strongly suggest you start with a simple web framework/library like Python Flask.

Just to answer some other things which will follow, I would strongly recommend you to catch up on MVC/MVP (Model View Controller/Presenter) which is a design pattern used for creating and consuming views.

abheyogy
  • 26
  • 2
  • I'm using Asp.net with c# and I already started with my web api. I can make GET request and all. I'm just not sure how to handle and obtain the Json from the stream. – batwing Aug 06 '18 at 09:31