0
votes

I am working with the Basecamp API which is a REST (JSON) API using basic HTTP authentication over HTTPS.

This should be a GET request but when I run my code using GET I am receiving:

Cannot send a content-body with this verb-type

When I run it as a POST, I receive:

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

Does anyone know why this may be occurring?

    using (var httpClient = new HttpClient()) {

        string userName = "[email protected]";
        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 ([email protected])");
        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.

1
Usually status 400 indicates that there was a problem with the payload that was sent. No so much the syntax but the business logic on the server did not like it (wrong parameter values, no parameters received, required parameter missing, etc). It usually very much depends on who implemented the service to make sure that the correct response type is returned (it might even be an authentication issue but wrong response code returned). Your best bet is to contact the owner of the API and ask them. - Igor
By the way, is this the API you are talking about? You should try their curl example and see if you can connect that way with your credentials, that way you have a complete self contained example you could give them if you are already getting stuck there. Also the only thing I read about 400 on their website is if you do not supply user-agent (which you do) it appears but maybe its not recognized in their system. - Igor

1 Answers

1
votes

GET requests must pass their parameters as url query and not as request body.

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).