【问题标题】:How to get response from API after POSTPOST后如何从API获取响应
【发布时间】:2015-11-24 00:56:28
【问题描述】:

我正在与一个 API 交互,该 API 有时会为某些函数返回一个参考号。我将如何显示此参考编号。目前API返回例如这个字符串(出于安全原因,一些代码被取出)。

if (AppSettings.GetLandingPageReceipts())
{
   long returnValue = loanLP.AddWithdrawalToBankID(CustomerID, SavingsAccountID, BankAccID, Amount);
   return string.Concat("Requested Withdrawal Ref No: ", returnValue);
}

ReceiptWithdrawal withdraw = new ReceiptWithdrawal(CustomerID, SavingsAccountID, Amount, session, pp, BankAccID);

ReceiptTRN TRN = new ReceiptTRN();

TRN.Post(withdraw);

return JsonConvert.SerializeObject(withdraw.ReceiptNo);

在 Ruby 代码中,我将其用于 Post 调用

def post_call(routePath, params)
  started_at = Time.now
  logger.bench 'SERVICE - POST', started_at, routePath   
  uri = URI.parse("#{settings.abacus_service_endpoint}#{routePath}") 
  http = Net::HTTP.new(uri.host, uri.port)
  http.set_debug_output $stderr
  req = Net::HTTP::Post.new(uri.request_uri,get_header_for_abacus_service)
  req.form_data = params
  resp = http.request(req) 
  if response_ok?(resp)
    if @errors
      puts '======= post request form_data ======='
      puts params
      puts '==================='
    end
    return resp 
  end
end

在本地工作时,我可以在控制台中看到以下内容,因此我知道它已通过。它只是访问它是问题。

opening connection to 10.10.10.27...
opened
<- "POST /Accounts/WithdrawalToBank HTTP/1.1\r\nAuthorization: Token 433\r\nX-Customer-Id: 433\r\nX-Customer-Pin: 8EFD155E7C829421E16F14D367568C4179C4548320CD7E1B6AD6E9A485F2AF092FA42D0F645085F3DBBA5AEC2434720FC76E407E41443C3F5EDAFB958793254A\r\nX-Ip-Address: 10.0.2.2\r\nAccept: */*\r\nUser-Agent: Ruby\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: close\r\nHost: 10.10.10.27:3579\r\nContent-Length: 81\r\n\r\n"
<- "CustomerID=433&SavingsAccountID=10922&BankAccountID=2450&Amount=10.00&Reference=a"
-> "HTTP/1.1 200 OK\r\n"
-> "Transfer-Encoding: chunked\r\n"
-> "Content-Type: text/html\r\n"
-> "Server: Microsoft-HTTPAPI/2.0\r\n"
-> "Date: Mon, 23 Nov 2015 11:03:02 GMT\r\n"
-> "Connection: close\r\n"
-> "\r\n"
-> "22\r\n"
reading 34 bytes...
-> "Requested Withdrawal Ref No: 10059"
read 34 bytes
reading 2 bytes...
-> "\r\n"
read 2 bytes
-> "0\r\n"
-> "\r\n"
Conn close

如果需要任何进一步的信息,请告诉我。

【问题讨论】:

    标签: ruby api


    【解决方案1】:

    如果 API 返回一个正文,您应该能够通过 resp.body 的响应获得该正文

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多