【问题标题】:Error in calling Microsoft vision API from C++从 C++ 调用 Microsoft Vision API 时出错
【发布时间】:2018-03-04 03:44:35
【问题描述】:

我想从 C++ 调用 Microsoft Vision API,我正在使用 cpr library 发出请求。现在我运行以下代码:

#include <iostream>
#include <cpr/cpr.h>
#include <json.hpp>

int main(int argc, char** argv) {
    auto response = cpr::Post(
        cpr::Url{"https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze"},
        cpr::Body{{"url","https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg"}},
        cpr::Header{{"Ocp-Apim-Subscription-Key", "xxxxxxxx"}}
    );
    std::cout << response.status_code ;
    auto json = nlohmann::json::parse(response.text);
    std::cout << json.dump(4) << std::endl;
}

虽然代码是由cmake 运行的,所以make 是成功的。但是当我执行可执行文件时,出现以下错误:

terminate called after throwing an instance of 'std::length_error'
what():  basic_string::_M_create
Aborted (core dumped)

PS:Microsoft Vision API 的文档可以在 here 找到 所以,如果我做错了什么,请告诉我。另外,如果有人知道如何在 QtQuick 应用程序中发送 http 请求,请告诉我

【问题讨论】:

    标签: c++ microsoft-cognitive


    【解决方案1】:

    你可以试试这个吗?

    auto my_json = nlohmann::json::object({
        { "url","https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg" }
    });
    
    response = cpr::Post(
        cpr::Url{"https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze"},
        cpr::Body{ my_json.dump() },
        cpr::Header{{"Ocp-Apim-Subscription-Key", "XXXXXX"}}
    );
    

    【讨论】:

      猜你喜欢
      • 2018-06-23
      • 2017-08-10
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多