I want to send an object (DataSet) From the windows application to the .netcore API
something like this (Windows Application side)
public string submitRequest(Dataset request)
{
.
.
.
using (client)
{
client.Headers["Content-Type"] = "application/json; charset=utf-8";
byte[] result = client.UploadValues(Url, "POST", request);
return Encoding.UTF8.GetString(result);
}
}
(.netcore API)
public string submitRequest( values)
{
// processing values
}
I try to convert to json and try NameValueCollection but no result.
CodePudding user response:
It was simple
- On the client side
Create new object :
Class1 SentClass1 = new Class1 ();
Then serialize the object :
string json = JsonConvert.SerializeObject(SentClass1);
Then send the JSON (UploadString) using WebClient
using (WebClient client = new WebClient()){
client.Headers[HttpRequestHeader.ContentType]="application/json";
response = client.UploadString("ourUrl/SubmitRequest", json);
}
On the other hand - On the Server Side
[HttpPost]
[Route("SubmitRequest")]
public void SubmitRequest(Class2 recievedClass){
return recievedClass.processing()
}
This automatically maps data from (JSON converted from Class1) to Class2 if they have the names of the same attributes if not I can use an auto mapper.