2

我正在使用 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;
    }
}
4

1 回答 1

2

验证您的实体名称和属性架构名称,看起来它们是自定义但缺少发布者前缀,例如。new_

datetime 字段也应该有.000before Z

"transactiondate":"2018-01-26T03:00:00.000Z"

更新

字符串和日期时间数据类型应该用引号括起来。

Int、Decimal、Currency (Money) 数据类型不应包含在引号中。尝试这个:

"transactionamount":500
于 2018-01-25T12:59:10.630 回答