Я получаю сообщение об ошибке при попытке отправить запрос POST методу, который существует в WEB API, отправленный параметр всегда равен NULL, ниже я собираюсь отправить метод POST WEB API:
public string Post([FromBody]string value)
{
var jsonObject = JsonConvert.DeserializeObject(value);
return jsonObject.ToString();
}
Я уверен, что вышеуказанный метод работает, так как я тестировал его с помощью POSTMAN, ниже я публикую метод, который вызывает REST WEB API:
static void Main(string[] args)
{
string fullURL = "http:/localhost:54029/api/values";
//Creating a Webrequest with any URL.
var httpWebRequest = (HttpWebRequest)WebRequest.Create(fullURL);
httpWebRequest.ContentType = "application/json";
//Defining Method type that will be used with this webrequest
httpWebRequest.Method = "POST";
//Creating the JSON Object with the required properties.
var json = new JObject(
new JProperty("Id", "ee288h0emlya"),
new JProperty("isDone", "False"),
new JProperty("stage", "Preparation"),
new JProperty("status", "Done"));
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
//Writing to the web service, emptying and closing the stream.
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
//Get the response from the server.
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
//Saving the response to result variable.
var result = streamReader.ReadToEnd();
Console.WriteLine(result);
}
Когда я вызываю первый методпри использовании второго значения параметра всегда null , и поэтому я получаю пустой ответ и ошибку.
Может кто-нибудь помочь, пожалуйста?