首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有restc-cpp post数据断言错误的Rest API

具有restc-cpp post数据断言错误的Rest API
EN

Stack Overflow用户
提问于 2021-05-05 00:26:55
回答 1查看 60关注 0票数 0

我尝试使用restc-cpp库在C++中发布json数据,但出现了以下错误:

代码语言:javascript
复制
Assertion failed: false, file lib\restc-cpp\include\restc-cpp/SerializeJson.h, line 1273

我遵循了此页面上的示例Post data to the server

代码语言:javascript
复制
struct Post
{
   int i;
};


// The C++ main function - the place where any adventure starts
int main() {

    Post post;
    post.i = 22;

    // Create an instance of the rest client
    auto rest_client = RestClient::Create();

    // Create and instantiate a Post from data received from the server.
    auto done = rest_client->ProcessWithPromise([&](Context& ctx)
    {
        // This is a co-routine, running in a worker-thread


       auto reply = RequestBuilder(ctx)
                 .Post("http://ptsv2.com/t/ywexb-1620143951/post")

                 .Data(post)
                 // Send the request
                 .Execute();

       cout << "GOT: " << reply->GetBodyAsString() << endl;

    });

    try
    {
    // Get the Post instance from the future<>, or any C++ exception thrown
    // within the lambda.
       done.get();
    }
    catch(const exception& ex)
    {
       cout << "Main thread: Caught exception from coroutine: "
             << ex.what() << endl;
    }

该错误发生在"SerializeJson.h“文件的以下函数中的"post”数据结构序列化期间

代码语言:javascript
复制
template <typename dataT, typename serializerT>
void do_serialize(const dataT& object, serializerT& serializer,
                const serialize_properties_t& properties,
    typename std::enable_if<
        !boost::fusion::traits::is_sequence<dataT>::value
        && !std::is_integral<dataT>::value
        && !std::is_floating_point<dataT>::value
        && !std::is_same<dataT, std::string>::value
        && !is_container<dataT>::value
        && !is_map<dataT>::value
        >::type* = 0) {
    assert(false);
};

行1273在assert上。

有谁有主意吗?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-05 17:53:06

我已经找到了错误。我已经阅读了type_traits标准库中关于enable_if的文档。这似乎是一张integral_type,floating_point,...的支票。在……里面

代码语言:javascript
复制
...
typename std::enable_if<
        !boost::fusion::traits::is_sequence<dataT>::value
        && !std::is_integral<dataT>::value
        && !std::is_floating_point<dataT>::value
        && !std::is_same<dataT, std::string>::value
        && !is_container<dataT>::value
        && !is_map<dataT>::value
        >::type* = 0)
...

在我的原始代码中,我为数据函数提供了一个Post结构

代码语言:javascript
复制
Post post;
post.i = 22;
...
auto reply = RequestBuilder(ctx)
                 .Post("http://ptsv2.com/t/ywexb-1620143951/post")

                 .Data(post)
                 // Send the request
                 .Execute();

我直接尝试了一个"int“,它是可以的。然后,我已经读取了restc-cpp doc,并在BOOST_FUSION_ADAPT_STRUCT结构定义之后的结构中添加了带有字符串的STRUCT宏:

代码语言:javascript
复制
struct Post
{
   int i;
   string name;
};

BOOST_FUSION_ADAPT_STRUCT(
    Post,
    (int, i)
    (string, name)
)

并且post请求成功了!

我希望它能帮助其他人!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67388587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档