I am trying to send data to a API it works fine using Postman but I am not sure how to do it in code.
I created this method that is using test json string and it send the data in the body. When I debug it the error is coming from this line
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
and I get the message
The remote server returned an error: (400) Bad Request.' "
Any help will be great not sure what I am doing wrong.
public static T Post<T>(string httpMethod, string url, object model)
{
var fullUrl = apiUrl + url;
var json = "{\"FirstName\":\"1\",\"LastName\":\"1\"}";
string data = json;
Stream dataStream = null;
WebRequest Webrequest;
Webrequest = WebRequest.Create(fullUrl);
Webrequest.ContentType = "application/json";
Webrequest.Method = WebRequestMethods.Http.Post;
Webrequest.PreAuthenticate = true;
Webrequest.Headers.Add("Authorization", "Bearer Toeke n");
byte[] byteArray = Encoding.UTF8.GetBytes(data);
Webrequest.ContentLength = byteArray.Length;
dataStream = Webrequest.GetRequestStream();
using (dataStream = Webrequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
WebResponse response = Webrequest.GetResponse();
}
API
[HttpPost]
public IHttpActionResult SaveForm(RequestWeb Request)
Postman Raw
{
"FirstName": "1",
"LastName": "1",
}