使用CPPREST SDK将图像上载到HipChat

使用CPPREST SDK将图像上载到HipChat,rest,hipchat,cpprest-sdk,Rest,Hipchat,Cpprest Sdk,我正在尝试使用CPPREST SDK将图像上载到HipChat,但未成功。有一些将图像上传到其他服务器的例子,但是HipChat API似乎更复杂(这是非常新的,我无法填补空白…) 从HipChat REST API文档(): 与房间共享一个文件 将请求格式化为multipart/与内容类型application/json的单个部分和包含文件的第二部分相关 注: 包含文件的部件必须在部件的内容处置标头中包含name=“file”。 包含消息的application/json部分是可选的,可以排除

我正在尝试使用CPPREST SDK将图像上载到HipChat,但未成功。有一些将图像上传到其他服务器的例子,但是HipChat API似乎更复杂(这是非常新的,我无法填补空白…)

从HipChat REST API文档():

与房间共享一个文件

将请求格式化为multipart/与内容类型application/json的单个部分和包含文件的第二部分相关

注: 包含文件的部件必须在部件的内容处置标头中包含name=“file”。 包含消息的application/json部分是可选的,可以排除,但需要文件部分

请求示例:

标题:

内容类型:多部分/相关;边界=边界123456

正文:

--边界123456 内容类型:application/json;字符集=UTF-8

内容处置:附件;name=“元数据”

{“消息”:“签出此文件上载!”}

--边界123456 内容类型:图像/png

内容处置:附件;name=“file”;filename=“upload.png”

“文件内容在此处”

--边界123456--

我正在尝试使用set_body()方法: void web::http::http_请求::set_正文(const并发::streams::istream&stream,…) 但是我不知道如何将文件流插入上述所有复杂的主体中。set_body()的文档说明: “这不能与设置请求正文的任何其他方法结合使用”。我是否需要将文件读入字符串,并将其嵌入到“文件内容在此显示”的位置,并使用其他set_body()方法之一,而不是对文件流使用set_body()方法

谢谢, Ofer

供参考

/**
 * Share file with room
 * More info: https://www.hipchat.com/docs/apiv2/method/share_file_with_room
 *
 * @param string $id The id or name of the room
 * @param array $content Parameters be posted for example:
 *                              array(
 *                                'name'                => 'Example name',
 *                                'privacy'             => 'private',
 *                              )
 *
 * @return \Psr\Http\Message\ResponseInterface
 * @throws
 */
public function sharefileWithRoom($id, $file)
{
    $url = $this->baseUrl . "/v2/room/{$id}/share/file";
    $headers = array(
        'Authorization' => $this->auth->getCredential()
    );
    $parts[] = [
        'headers' => [
            'Content-Type' => $file['file_type'] ?: 'application/octet-stream',
        ],
        'name' => 'file',
        'contents' => stream_for($file['content']),
        'filename' => $file['file_name'] ?: 'untitled',
    ];
    if (! empty($file['message'])) {
        $parts[] = [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'name' => 'metadata',
            'contents' => json_encode(['message' => $file['message']]),
        ];
    }
    return $response =  $this->postMultipartRelated($url, [
            'headers' => $headers,
            'multipart' => $parts,
        ]);
}
/**
 * Make a multipart/related request.
 * Unfortunately Guzzle doesn't support multipart/related requests out of the box.
 *
 * @param $url
 * @param $options
 * @return \Psr\Http\Message\ResponseInterface
 */
protected function postMultipartRelated($url, $options)
{
    $headers = isset($options['headers']) ? $options['headers'] : [];
    $body = new MultipartStream($options['multipart']);
    $version = isset($options['version']) ? $options['version'] : '1.1';
    $request = new Request('POST', $url, $headers, $body, $version);
    $changeContentType['set_headers']['Content-Type'] = 'multipart/related; boundary='.$request->getBody()->getBoundary();
    $request = modify_request($request, $changeContentType);
    $client = new HttpClient;
    return $client->send($request);
}
供参考

班级