【问题标题】:Getting "BadRequest" when posting data to SharePoint Online list with HttpClient使用 HttpClient 将数据发布到 SharePoint Online 列表时出现“BadRequest”
【发布时间】:2017-11-13 11:56:00
【问题描述】:

我正在尝试使用 C# HttpClient 将数据发布到 SharePoint Online 上的列表。这是我的代码:

  using (var client = new SPHttpClient(webUri, userName, password))
        {
            var listTitle = "HttpClientList";
            var itemPayload = new
            {
                __metadata = new
                {
                    type = "SP.Data.HttpClientListListItem"
                },
                Title = "test3",<--column name "Title"
                _x0071_cr5 = "value3"<--column name "Value"
            };
            var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items", webUri, listTitle);
            var data = client.ExecuteJson(endpointUrl, HttpMethod.Post, itemPayload);
            Console.WriteLine("Task item '{0}' has been created", data["d"]["Title"]);
            Console.ReadLine();

截至目前,我的状态为“400,BadRequest”。我的猜测是我遗漏了一些东西或为帖子请求提供了错误的数据。 我一直在关注这篇博文Blogpost 我只能执行动词 GET。所有其他动词都给我“400,BadRequest”

【问题讨论】:

  • 您是否尝试检查响应正文中是否有更多详细信息。
  • 我有一些进展:我现在可以创建和更新数据,但仅限于 Title 列,Cant create or update more than one column。我仍然无法删除项目。找不到响应的正文。只能找到:“RequestMessage”、“StatusCode”、“Headers”、“EnsureSuccessStatusCode”、“IsSuccessStatusCode” 也许我在看正文的错误位置
  • 你从响应内容中取出正文。body = await response.Content.ReadAsStringAsync()
  • 博文中提到了删除问题是cmets
  • 是的!你说的太对了。删除工作。仅发布并放入左侧的多列

标签: c# dotnet-httpclient sharepoint-online


【解决方案1】:

用各种解决方案解决了这个问题。

添加了正确的 ListItem HttpClientListListItem。 通过 ListSettings 面板创建了所有列。 将client.ExecuteJson() 替换为client.ExecuteJson(endpointUrl, HttpMethod.Post, headers, default(string));。 感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2017-04-16
    • 2021-02-12
    • 2018-09-14
    相关资源
    最近更新 更多