[英]https request--Python code to C++
我有一個用Python編寫的函數,該函數調用Robinhood(股票交易經紀人)API來獲取報價數據(以下代碼片段中的“ get_quote(self,symbol)”函數)。 而且效果很好。 返回了正確的市場數據。
import requests
import urllib
class Robinhood(object):
# All known endpoints as of September 5th, 2015
endpoints = {
"quotes": "https://api.robinhood.com/quotes/",
"user": "https://api.robinhood.com/user/",
"user/additional_info": "https://api.robinhood.com/user/additional_info/",
"user/basic_info": "https://api.robinhood.com/user/basic_info/",
"user/employment": "https://api.robinhood.com/user/employment/",
"user/investment_profile": "https://api.robinhood.com/user/investment_profile/",
"watchlists": "https://api.robinhood.com/watchlists/"
}
def get_quote(self, symbol):
''' Returns a qoute object for a given symbol including all data returned by Robinhood's API'''
data = { 'symbols' : symbol }
res = self.session.get(self.endpoints['quotes'], params=data)
if res.status_code == 200:
return res.json()['results']
else:
raise Exception("Could not retrieve quote: " + res.text)
我嘗試使用Curl庫在C ++中實現此邏輯。 但這是行不通的。 沒有編譯或運行時錯誤,但是程序返回了一個無法讀取的字符,而不是股票的市場價格。 在我看來,我的網址設置不正確,但我不知道如何解決它。 有人有主意嗎? 謝謝!
std::string RobinhoodAPI::GetQuote(std::string ticker)
{
struct response resStr;
init_string(&resStr);
std::string url = "https://api.robinhood.com/quotes/symbols=AVP/";
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resStr);
resCode = curl_easy_perform(curl);
std::cout << std::string(resStr.ptr);
return std::string(resStr.ptr);
}
我為robinhood api的非官方文檔創建了一個開放的api規范。 這樣,您可以為大多數語言生成http客戶端。
請訪問此處https://github.com/sabareeshkkanan/robinhood了解該規范。 並請訪問此倉庫以了解如何使用此規范生成客戶端https://github.com/swagger-api/swagger-codegen
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.