簡體   English   中英

ASP.NET 核心 Web API - 第三方 API 未給出預期結果

[英]ASP.NET Core Web API - Third Party API not giving expected result

在我的 ASP.NET Core-6 Web API 給我一個第三方 API 消費然后返回賬戶明細。 我正在使用網絡客戶端。

api:

https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber=112123412

標頭:

X-GivenID:Given2211
X-GivenName:Givenyou
X-GivenPassword:Given@llcool

然后 JSON 結果如下圖:

{
  "AccountName": "string",
  "CurrentBalance": 0,
  "AvailableBalance": 0,
  "Currency": "string"
}

到目前為止,我已經這樣做了:

余額查詢響應:

public class BalanceEnquiryResponse
{
    public string Response
    {
        get;
        set;
    }

    public bool IsSuccessful
    {
        get;
        set;
    }

    public List<BalanceList> AccountBalances
    {
        get;
        set;
    }
}

余額列表:

public class BalanceList
{
    public string AccountNumber
    {
        get;
        set;
    }

    public decimal CurrentBalance
    {
        get;
        set;
    }

    public decimal AvailableBalance
    {
        get;
        set;
    }

    public string Currency
    {
        get;
        set;
    }
}

然后服務如下圖所示。

數據服務:

public interface IDataService
{
    BalanceEnquiryResponse GetAccountBalance(string accountNo);
}
public class DataService : IDataService
{
    private readonly ILogger<DataService> _logger;
    private readonly HttpClient _myClient;
    public DataService(ILogger<DataService> logger, HttpClient myClient)
    {
        _logger = logger;
        _myClient = myClient;
        PrepareAPIHeaders(); // Actually apply the headers!
    }

    private void PrepareAPIHeaders()
    {
        _myClient.DefaultRequestHeaders.Add("X-GivenID", "Given2211");
        _myClient.DefaultRequestHeaders.Add("X-GivenName", "Givenyou");
        _myClient.DefaultRequestHeaders.Add("X-GivenPassword", "Given@llcool");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
        _myClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json; charset=utf-8");
    }

    // If you want to use async API, you need to go async all the way.
    // So make this Method async, too!
    public async Task<BalanceEnquiryResponse> GetAccountBalance(string accountNo)
    {
        _logger.LogInformation("Accessing Own Account");
        var url = $"https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber={accountNo}";

        var response = await _myClient.GetAsync(url);
        // vv Get your payload out of the Http Response.
        var responseResults = await response.Content.ReadAsAsync<BalanceEnquiryResponse>();
        return responseResults;
    }
}

我用 POOSTMAN 上的標頭測試了第三方 api:

https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber=112123412

范圍

標頭

它給了我預期的結果。 但是根據我的代碼,當我嘗試從下面的代碼調用GetAccountBalance時,我提供了 model.account_number:

public async Task<BaseResponse> FinalResult(RequestDto model)
{
    var response = new BaseResponse();
    try
    {
        //Check account Balance
        var accBalance = _dataAccess.GetAccountBalance(model.account_number);
        if (!accBalance.IsSuccessful)
        {
            response.response_code = "";
            response.response_description = "Could not fetch account for subscriber";
            return response;
        }
     }
}

我在以下位置收到此錯誤:

response.response_description = "無法獲取訂閱者的帳戶";

我做錯了什么,尤其是在public class DataService中,我該如何解決?

謝謝。

可能只是我們在談論 GetCall 但有時我遇到直接在客戶端層設置的標頭的問題。 所以我會嘗試根據請求設置它們,而不是客戶端。

我在沒有編輯的情況下寫了這篇文章,所以我無法測試它。 但你應該了解我想做的事情的要點


 public async Task<BalanceEnquiryResponse> GetAccountBalance(string accountNo)
 {
        _logger.LogInformation("Accessing Own Account");
        var url = $"https://api.thirdpartycompany.com:2233/UserAccount/api/AccountDetail?accountNumber={accountNo}";
       using(var request = new HttpRequestMessage(HttpMethod.Get, url))
       {
            //add headers to the request not the client
            PrepareAPIHeaders(request);
            var result = await _myClient.SendAsync(request);
            result.EnsureSuccessStatusCode();
            
            /*Read response and parse it into an object and return*/
       }


        return null;
 }

    private void PrepareAPIHeaders(HttpRequestMessage request)
    {
        request.Headers.Add("X-GivenID", "Given2211");
        /*Add other Headers*/
    }

暫無
暫無

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

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