Hi Friends,

I am developing a plugin on create / Update of lead to post the lead to a mendix application API using Json and basic authentication. I have tried to create the post method using HttpClient, RestClient and HttpWebResponse classes and all these method will work from my unit test method. While I try from dynamics 365 sandbox it returns an internal server error. Would you please help me to fix this?

The dynamics version we are using is dyamics 365 Online.

The url to post I used is not an ip address, it is a valid domain name only.

The method I tried using HttpWebResponse is the following.

public HttpStatusCode PostWebRequest(SachaCustomer customer)

HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(_url);

Encoding encoding = new UTF8Encoding();
string postData = JsonConvert.SerializeObject(customer);
byte[] data = encoding.GetBytes(postData);

httpWReq.ProtocolVersion = HttpVersion.Version11;
httpWReq.Method = "POST";
httpWReq.ContentType = "application/json";
string _auth = string.Format("{0}:{1}", userName, password);
string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
string _cred = string.Format("{0} {1}", "Basic", _enc);
httpWReq.Headers[HttpRequestHeader.Authorization] = _cred;
httpWReq.ContentLength = data.Length;
Stream stream = httpWReq.GetRequestStream();
stream.Write(data, 0, data.Length);

HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string s = response.ToString();
StreamReader reader = new StreamReader(response.GetResponseStream());
String jsonresponse = "";
String temp = null;
while ((temp = reader.ReadLine()) != null)
jsonresponse += temp;
return response.StatusCode;
catch (WebException e)
//throw new InvalidPluginExecutionException(e.Message);
using (WebResponse response = e.Response)
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream data = response.GetResponseStream())
using (var reader = new StreamReader(data))
string text = reader.ReadToEnd();

return httpResponse.StatusCode;


The method using HttpClient is the following

public async Task<HttpStatusCode> SendRequestAsync(SachaCustomer rootCustomer)
using (HttpClient httpClient = new HttpClient())
Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(_authCode));
httpClient.DefaultRequestHeaders.Add("Accept", "application/json, text/javascript, */*; q=0.01");
StringContent httpConent = new StringContent(JsonConvert.SerializeObject(rootCustomer), System.Text.Encoding.UTF8, "application/json");

HttpResponseMessage responseMessage = null;
responseMessage = await httpClient.PostAsync(new Uri(_url), httpConent);
catch (Exception ex)
throw new InvalidPluginExecutionException(ex.Message);

return responseMessage.StatusCode;
//return responseMessage;