libcurl post - 用空格替换下划线(ubuntu)

时间:2018-03-22 07:14:03

标签: c++ express curl

我正在尝试使用curl c ++在post请求中向我的express nodejs服务器发送电子邮件和密码。当我从服务器登录时,帖子数据的'_'已更改为''。

    char emailtext[50];
    char passwordtext[50];
    int emailstrlen = wcstombs(emailtext, email->getText(), 50);
    int passwordstrlen = wcstombs(passwordtext, password->getText(), 50);
    long totalsize = emailstrlen + passwordstrlen;
    strcat(emailtext, ":");
    strcat(emailtext, passwordtext);
    // "myemail@yahoo.com:mypassword\0"
    curl_global_init(CURL_GLOBAL_ALL);
    CURLcode res;
    CURL* curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL, "https://localhost:3000/login");
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
    struct curl_slist *headers=NULL;
    headers = curl_slist_append(headers, "Content-Type:text/plain; charset=utf-8");
    cout << emailtext << endl;
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, emailtext);
    /* pass our list of custom made headers */
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    curl_easy_perform(curl); /* post away! */ 
    curl_slist_free_all(headers); /* free the header list */
    curl_global_cleanup();
    return true;

表达nodejs服务器:

    app.post('/login', bodyParser.text(), function (req, res) {
         console.log("we got the post request for /login");
         console.log("logging the body!");
         console.log(req.body);
         res.header('Content-type', 'text/plain');
         return res.end('<h1>Hello, Secure World!</h1>');
    });

说char * emailtext =“cool_stewj@yahoo.com:lololol”

从登录服务器输出的

将是“很酷的stewj@yahoo.com:lololol”

发生了什么事?我试过url编码数据,结果证明是愚蠢无意义的。我如何得到这个下划线?

1 个答案:

答案 0 :(得分:0)

ubuntu中的终端显然没有显示下划线,因此任何日志输出都会将下划线替换为空格。

顺便说一句,这是一个糟糕的字符串代码示例。可以只使用一个缓冲区。只是指出来。