我正在使用 CRM Web API 来执行 CRUD 操作。
字段列表
无论在哪里Disabled
设置,都是为了字段安全。
对于POST
请求,当我使用以下代码执行此请求时,它会抛出Bad Request 错误。我的代码示例如下:
网址 https://BaseURL/api/data/v8.2/transactions
json数据
{
"transactionnumber":"123456789123",
"transactionamount":"500",
"transactiondate":"2018-01-26T03:00:00.000Z"
}
代码:
public Dictionary<string, string> ExecutePostRequest(string entityName, string jsonFormattedData)
{
string requestURL = GenerateRequestURL(entityName);
HttpContent content = new StringContent(jsonFormattedData, Encoding.UTF8, "application/json");
return DoRequest(requestURL.ToString(), content, HttpMethod.Post);
}
DoRequest 方法 //实际执行http请求
private Dictionary<string, string> DoRequest(string requestUrl, HttpContent content, HttpMethod httpMethod)
{
Dictionary<string, string> returnValue;
HttpResponseMessage responseMessage;
returnValue = new Dictionary<string, string>();
try
{
HttpClient httpClient = SetUpCRMConnection();
HttpRequestMessage request;
request = new HttpRequestMessage(httpMethod, requestUrl);
switch (httpMethod.ToString())
{
case "PATCH":
case "POST":
case "PUT":
request.Content = content;
break;
case "DELETE":
break;
}
responseMessage = httpClient.SendAsync(request).Result;
return GetFormattedResponse(responseMessage);
}
catch (UriFormatException ex)
{
logger.Error(ex.InnerException);
returnValue.Add("ERROR", "Invalid URL generated: " + ex.InnerException.ToString());
return returnValue;
}
catch(Exception ex)
{
logger.Error(resourceManager.GetString("CRM_ConnectionLost"),ex);
returnValue.Add("ERROR", resourceManager.GetString("CRM_ConnectionLost"));
return returnValue;
}
}