
I am trying to Create Record in Account using Web Api.
Issue is Im getting 400 bad request if Iam using it from FROMBODY
This is my request given in postman
This is my Post method
[HttpPost]
[Route("api/account/post")]
public async Task<string> Post([FromBody]Account account)
{
try
{
string accessToken = authenticateAsync();
HttpClient httpClient = new HttpClient();
//Default Request Headers needed to be added in the HttpClient Object
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Set the Authorization header with the Access Token received specifying the Credentials
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
httpClient.BaseAddress = new Uri(webApiUrl);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "accounts")
{
Content = new StringContent(JsonConvert.SerializeObject(account), Encoding.UTF8, "application/json")
};
using (HttpResponseMessage response = await httpClient.SendAsync(request))
{
if (response.IsSuccessStatusCode) //204
{
var entityUri = response.Headers.GetValues("OData-EntityId").FirstOrDefault();
return entityUri;
}
}
return string.Empty;
}
catch (Exception e)
{
throw e;
}
Hi,
Please find the information using below links:
http://raju2565.blogspot.com/2019/02/create-records-using-web-api-c-in.html
https://stackoverflow.com/questions/53812242/creating-dynamics-365-entity-record-using-webapi-only
https://www.dotnetfunda.com/codes/show/8372/ms-dynamics-365-create-records-using-ms-dynamics-web-api
Best Regards,
Mobeen