简体   繁体   English

REST JSON GET-400错误的请求

[英]REST JSON GET - 400 Bad Request

I am working with the Basecamp API which is a REST (JSON) API using basic HTTP authentication over HTTPS. 我正在使用Basecamp API,它是使用HTTPS上的基本HTTP身份验证的REST(JSON)API。

This should be a GET request but when I run my code using GET I am receiving: 这应该是一个GET请求,但是当我使用GET运行代码时,我收到了:

Cannot send a content-body with this verb-type 无法发送具有这种动词类型的内容主体

When I run it as a POST, I receive: 当我将其作为POST运行时,我收到:

{"status":"400","error":"Bad Request"} {“状态”:“ 400”,“错误”:“错误请求”}

Does anyone know why this may be occurring? 有谁知道为什么会这样?

    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);
    }

In this code I obviously swapped out the username, password, project name, and URL but in the actual code they are all correct. 在这段代码中,我显然换出了用户名,密码,项目名称和URL,但在实际代码中,它们都是正确的。

GET requests must pass their parameters as url query and not as request body. GET请求必须将其参数作为url查询而不是请求主体传递。

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

If you don't have any content, as your example suggests, omit setting it on the request. 如您的示例所示,如果您没有任何内容,请忽略在请求上进行设置。

The BadRequest result indicates some error with your payload (again: content seems to be empty). BadRequest结果表明您的有效负载存在一些错误(再次:内容似乎为空)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM