簡體   English   中英

REST JSON GET-400錯誤的請求

[英]REST JSON GET - 400 Bad Request

我正在使用Basecamp API,它是使用HTTPS上的基本HTTP身份驗證的REST(JSON)API。

這應該是一個GET請求,但是當我使用GET運行代碼時,我收到了:

無法發送具有這種動詞類型的內容主體

當我將其作為POST運行時,我收到:

{“狀態”:“ 400”,“錯誤”:“錯誤請求”}

有誰知道為什么會這樣?

    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結果表明您的有效負載存在一些錯誤(再次:內容似乎為空)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM