【问题标题】:Send POST multipart/form-data request using restbed C++使用 restbed C++ 发送 POST 多部分/表单数据请求
【发布时间】:2018-02-04 15:19:12
【问题描述】:

我正在使用 restbed 库开发 C++ REST 客户端,该库将使用 POST 请求发送 base64 编码图像。 到目前为止我写的代码是:

auto request = make_shared< Request >(Uri("http://127.0.0.1:8080/ProcessImage"));
request->set_header("Accept", "*/*");
request->set_header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request->set_header("Cache-Control", "no-cache");
request->set_method("POST");
string test = "------WebKitFormBoundary7MA4YWxkTrZu0gW"
    "Content-Disposition:form-data;name=\"image\""
    ""
    "testMessage"
    "------WebKitFormBoundary7MA4YWxkTrZu0gW--";
request->set_body(imgContent);
auto response = Http::sync(request)

我不确定应该如何设置请求正文。我尝试使用简单的 image="blabla" 以及我从邮递员那里收到的长版消息。 但在每种情况下,我都会收到“错误 400 错误请求”的答案。

更新: 也使用此版本的代码进行了测试,但没有成功:

auto request = make_shared< Request >(Uri("http://127.0.0.1:8080/ProcessImage"));
    request->set_header("Accept", "*/*");
    request->set_header("Host","127.0.0.1:8080");
    request->set_method("POST");
    request->set_header("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
    request->set_header("Cache-Control", "no-cache");
    string imgContent = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n"
        "Content-Disposition: form-data; name=\"image\"\r\n"
        "\r\n"
        "test\r\n"
        "------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n";
    request->set_body(imgContent
    auto response = Http::sync(request);

我从服务器得到的响应:

    *** Response ***
Status Code:    400
Status Message: BAD REQUEST
HTTP Version:   1.0
HTTP Protocol:  HTTP
Header 'Content-Length' > '192'
Header 'Content-Type' > 'text/html'
Header 'Date' > 'Sun, 04 Feb 2018 21:09:45 GMT'
Header 'Server' > 'Werkzeug/0.14.1 Python/3.5.4'
Body:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
²²²²∩x...

我还在服务器端(使用 python 烧瓶)添加了: encoded_img = request.form.get('image') 并打印字符串。打印结果为:“无”

【问题讨论】:

    标签: c++ rest multipartform-data restbed


    【解决方案1】:

    您的正文内容在每行末尾缺少明确的换行符。 C++ 不会自动为您插入它们。

    另外,如果您要发送 base64 数据,您也应该包含一个 Content-Transfer-Encoding 标头。

    试试这个:

    string imgContent = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n"
        "Content-Disposition: form-data; name=\"image\"\r\n"
        "Content-Transfer-Encoding: base64\r\n"     
        "\r\n"
        "<base64 image data here>\r\n"
        "------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n";
    request->set_body(imgContent);
    

    【讨论】:

    • 感谢您的回复。我尝试了您的版本,但不幸的是我仍然收到错误 400。现在该消息看起来与邮递员的消息相同。也许我错过了一个标题或其他东西,或者我错误地添加了一个。我用我使用的代码版本更新了帖子。
    • @Alex 标题和内容看起来不错。您能显示实际传输到服务器的原始请求数据吗?也许Http::sync 添加了一些错误的东西。服务器端是否记录了任何内容来解释它认为该请求的坏处? 400 响应中是否有任何正文内容解释了问题?
    • 这是我得到的回复:*** 响应 *** 状态代码:400 状态消息:错误请求 HTTP 版本:1.0 HTTP 协议:HTTP 标头“内容长度”>“192”标头“内容类型”>“文本/html”标题“日期”>“星期日,2018 年 2 月 4 日 21:09:45 GMT”标题“服务器”>“Werkzeug/0.14.1 Python/3.5.4”正文: 400 Bad Request

      Bad Request

      浏览器(或代理)发送了一个请求这个服务器无法理解。

      ²²²²∩x...
    • 我还在服务器端(使用 python 烧瓶)添加了:encoded_img = request.form.get('image') 并打印了字符串。打印结果是:“无”我需要看看如何检查原始请求,因为确实可能 Http::sync 向请求添加了错误。
    • 最后我设法打印了请求,它看起来与一个 fomr 邮递员相同。但是来自邮递员的作品和我写的作品没有。我不知道问题出在哪里,但我选择了另一种方法。
    猜你喜欢
    • 2022-12-05
    • 2012-11-27
    • 1970-01-01
    • 2017-10-05
    • 2018-03-05
    • 2020-06-05
    • 2011-10-03
    相关资源
    最近更新 更多