简体   繁体   English

RestSharp:发布请求返回“方法不允许”

[英]RestSharp: Post Request returning "Method Not Allowed"

Using RestSharp to Method.Post Login Details to return a String.使用 RestSharp 到 Method.Post 登录详细信息以返回字符串。 The API is returning "Method Not Allowed". API 返回“不允许的方法”。 In other parts of my application I have used RestSharp for different API's and they are working perfectly fine!在我的应用程序的其他部分,我将 RestSharp 用于不同的 API,它们工作得非常好! So any suggestions on what I should try will be greatly appreciated!因此,任何关于我应该尝试的建议都将不胜感激!

This is the source code.这是源代码。

private async Task<bool> APIAuthenticate()
{
    string URL;
    RestClient client;
    RestRequest request;
   
    if (string.IsNullOrWhiteSpace(aiAuthToken))
    {
        Logging.Info("AskTheExpertFindCodes : Authenticating");

        string username = "UserNameExample";
        string password = "PasswordExample";

        URL = "https://APIExample/getToken";

        client = new RestClient(URL);
        request = new RestRequest("Gettoken", Method.Post);
        
        request.AddHeader("Accept", "application/json");
       

        request.RequestFormat = DataFormat.Json;
        
        request.AddJsonBody(new 
        { 
            username = $"{username}",
            password = $"{password}"
        });

        try
        {
            var response = await client.ExecuteAsync(request);

            var jsonResponse = response.Content.ToString();

            if (! jsonResponse.Contains("authtoken"))
            {
                aiAuthToken = "";
            } else
            {
                var oJSON = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic(jsonResponse);

                oJSON = oJSON["return"];
                aiAuthToken = oJSON["authtoken"];
            }
        }
        catch (Exception ex) { mFN.ShowError(this.Text, "AskTheExpert.Authenticate", ex.Message); }
}

This is the cURL command.这是 cURL 命令。

 curl -X 'POST' \
  'https://APIExample/getToken/' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "username": "string",
  "password": "string"
}'

I remember a similar issue with Cognito API, which was picky about the Accept header value set by RestSharp.我记得与 Cognito API 类似的问题,它对 RestSharp 设置的Accept header 值很挑剔。 It is solved in the last RestSharp pre-release version.它在最后一个 RestSharp 预发布版本中得到了解决。

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

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