0

I've created a public Data Entity in dynamics with the following fields:

enter image description here

I keep getting a bad request response, but I'm not sure why. I've tried to make a POST request in two ways:

1.

HireAction hireAction = new HireAction() { CompanyName = "DEMF", MovieId = "DEMF-000000014", HireActionStatus = "Created" };
                string jsonMessage = JsonConvert.SerializeObject(hireAction);
                using (HttpClient client = new HttpClient())
                {
                    HttpRequestMessage requestMessage = new
                    HttpRequestMessage(HttpMethod.Post, "MyDynamicsEnvironmentName/data/HireActions?cross-company=true");
                    requestMessage.Content = new StringContent(jsonMessage, Encoding.UTF8, "application/json");
                    requestMessage.Headers.Add("Authorization", AuthResult.AuthorizationHeader);
                    HttpResponseMessage response = client.SendAsync(requestMessage).Result;
                    if (response.StatusCode == System.Net.HttpStatusCode.OK)
                    {
                        //Logic
                    }
                }
            var url = "MyDynamicsEnvironmentName/data/HireActions?cross-company=true";
            var req = HttpWebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Headers["Authorization"] = AuthResult.AuthorizationHeader;

            HireAction hireAction = new HireAction() { CompanyName = "DEMF", MovieId = "DEMF-000000014", HireActionId = "12345", HireActionStatus = "Created" };
            var jsonSettings = new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Local
            };
            var postString = "CompanyName='DEMF'" + "&MovieId='DEMF-000000014'" + "&HireActionId=132&HireActionStatus='Created'";
            var data = JsonConvert.SerializeObject(postString, jsonSettings);
            var bytes = Encoding.Default.GetBytes(postString);
            var newStream = req.GetRequestStream(); 
            newStream.Write(bytes, 0, bytes.Length);
            newStream.Close();

            using (var resp = req.GetResponse())
            {
                var results = new StreamReader(resp.GetResponseStream()).ReadToEnd();
            }

Some keypoints:

-Of course you'd replace MyDynamicsEnvironmentName with the URL for the environment. The URL is correct and verified however, by the fact that GET requests do work

-The Authresult.AuthorizationHeader contains a valid token, also validated by working GET requests

As said before, both of these result in a bad request. Does someone know what is wrong or missing?

Kai
  • 732
  • 1
  • 7
  • 23
  • Can you get a valid posting via something like `curl`/Postman? If yes, then you can just run Fiddler and inspect what's being sent. – Alex Kwitny Nov 09 '22 at 15:32
  • Have you tried POST requests against other (standard) entities? – FH-Inway Nov 09 '22 at 22:18
  • @FH-Inway: I tried to POST if via postman now and this actually works! So it seems the error is not thrown by Dynamics but simply by me not setting up the POST request correctly – Kai Nov 10 '22 at 08:32

0 Answers0