REST JSON GET - 400 错误请求

REST JSON GET - 400 Bad Request

我正在使用 Basecamp API,这是一个 REST (JSON) API 使用基于 HTTPS 的基本 HTTP 身份验证。

这应该是一个 GET 请求,但是当我 运行 我的代码使用 GET 时,我收到:

Cannot send a content-body with this verb-type

当我 运行 它作为 POST 时,我收到:

{"status":"400","error":"Bad Request"}

有谁知道为什么会发生这种情况?

    using (var httpClient = new HttpClient()) {

        string userName = "someone@someone.com";
        string password = "somepassword";

        var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password)));

        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://correctUrlHere);
        requestMessage.Headers.Add("User-Agent", "TheProject (someone@someone.com)");
        requestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");

        var response = await httpClient.SendAsync(requestMessage);

        var responseContent = await response.Content.ReadAsStringAsync();

        Console.WriteLine(responseContent);
    }

在这段代码中,我显然换掉了用户名、密码、项目名称和 URL,但在实际代码中它们都是正确的。

GET 请求必须将其参数作为 url 查询而不是请求正文传递。

http://example.com?p1=1&p2=helloworld 

如果您没有任何内容,如您的示例所示,请忽略在请求中设置它。

BadRequest 结果表明您的负载存在一些错误(同样:内容似乎是空的)。