具有vibe.d的多份表单数据

问题描述 投票:0回答:1

我正在尝试提交包含带有vibe.d图像的多部分表单

我的代码如下:

    auto f = File("image.jpg");
    auto data = new char[f.size];
    f.rawRead(data);

    string boundary = randomUUID.toString.toUpper();

    string h = "--Boundary-" ~ boundary ~ "\r\n";
    h ~= `Content-Disposition: form-data; name="type"` ~ "\r\n\r\n";
    h ~= "photo\r\n";
    h ~= "--Boundary-" ~ boundary ~ "\r\n";
    h ~= `Content-Disposition: form-data; name="photo"; filename="558704D0-2855-4689-996C-F556BE4A3872.jpg"` ~ "\r\n";
    h ~= "Content-Type: image/jpeg\r\n\r\n";
    h ~= data ~ "\r\n";
    h ~= "--Boundary-" ~ boundary ~ "\r\n";
    h ~= `Content-Disposition: form-data; name="photo_ids"` ~ "\r\n\r\n";
    h ~= `["55227F15-36D2-4A04-A4D9-FB23C00627D1"]` ~ "\r\n";
    h ~= "--Boundary-" ~ boundary ~ "\r\n";


    auto response = requestHTTP("https://httpbin.org/anything", (scope req) {
        req.method = HTTPMethod.POST;
        req.headers["content-type"] = "multipart/form-data; boundary=Boundary-" ~ boundary;
        req.headers["content-length"] = to!string(h.length);

        req.bodyWriter.write(h);
    }).bodyReader.readAllUTF8();

    logInfo(response);

但是httpbin告诉我我什么也没张贴:

[main(----) INF] {
[main(----) INF]   "args": {},
[main(----) INF]   "data": "",
[main(----) INF]   "files": {},
[main(----) INF]   "form": {},
[main(----) INF]   "headers": {
[main(----) INF]     "Accept-Encoding": "gzip, deflate",
[main(----) INF]     "Content-Length": "58038",
[main(----) INF]     "Content-Type": "multipart/form-data; boundary=Boundary-76CCC942-83EB-4339-BB6B-2C7D5BF027B6",
[main(----) INF]     "Host": "httpbin.org",
[main(----) INF]     "User-Agent": "vibe.d/1.7.0 (HTTPClient, +http://vibed.org/)"
[main(----) INF]   },
[main(----) INF]   "json": null,
[main(----) INF]   "method": "POST",
[main(----) INF]   "origin": "",
[main(----) INF]   "url": "https://httpbin.org/anything"
[main(----) INF] }

我不知道我在做什么错。希望得到帮助

d vibed
1个回答
0
投票
last边界,您必须附加一个“-”字符串以指示结尾。因此,代替了[[last

h ~= "--Boundary-" ~ boundary ~ "\r\n"; 它必须是

h ~= "--Boundary-" ~ boundary ~ "--\r\n";`
然后它将起作用。

请参见https://www.freesoft.org/CIE/RFC/1521/16.htm

最终应该在vibe.d中将其作为API支持,并且为此有2个打开的PR,但是现在您必须像以前一样解决它。

https://github.com/vibe-d/vibe.d/pull/1178

https://github.com/vibe-d/vibe.d/pull/1876

© www.soinside.com 2019 - 2024. All rights reserved.