使用C ++ REST SDK发送HTTP POST请求以更新文件内容 [英] Sending a HTTP POST request for updating file contents using C++ REST SDK Casablanca

查看:183
本文介绍了使用C ++ REST SDK发送HTTP POST请求以更新文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C ++ rest sdk更新alfresco服务器中文件的内容。我正在使用露天CMIS url发送请求。更具体地说,它是Alfresco CMIS浏览器绑定。我必须坚持使用浏览器绑定而不是原子绑定。

I am trying to update the contents of a file in alfresco server using C++ rest sdk. I am using the alfresco CMIS url to send the request.To be more specific it is Alfresco CMIS browser binding.I have to stick to browser binding not atom binding.

何时我发送的请求总是更新文件的版本而不是内容。我在请求正文中发送内容。下面是我的代码

When I send the request it always updates the version of the file not the contents. I am sending the contents in the request body.Below is my code

void UpdateFileContent()
{
  concurrency::streams::basic_istream<unsigned char> fileStream = file_stream<unsigned char>::open_istream("C:\Desktop\Sample.txt").get();

  concurrency::streams::stringstreambuf streamBuffer;
  fileStream.read_to_end(streamBuffer).get();
  std::string textFile = move(streamBuffer.collection());
  fileStream.close();
  streamBuffer.close();
  std::string textBoundary = "--FORMBOUNDARY--";
  std::string textBody = "";
  textBody += "--" + textBoundary + "\r\n";
  textBody += "Content-Disposition:form-data;name=Sample;filename=Sample\r\n";
  textBody += "Content-Type: application/octet-stream\r\n\r\n";
  textBody +=textFile+"\r\n";
  textBody += "--" + textBoundary + "--\r\n";
  web::uri_builder builder(U("http://Alfresco-Server:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser/root/Sites/"));
  builder.append_path(U("siteName/documentLibrary/FolderName/Sample3.doc"));
  builder.append_query(U("alf_ticket"),ticket);
  builder.append_query(U("cmisaction"),U("update"));
  builder.append_query(U("propertyId[0]"),U("cmis:name"));
  builder.append_query(U("propertyValue[0]"),U("SampleFileUpdate"));
  http_client client(builder.to_string());
  http_request req;
  req.set_method(methods::POST);
  req.headers().set_content_type(L"multipart/form-data;boundary=--FORMBOUNDARY--");
  req.headers().set_content_length(textBody.length());
  req.set_body(textBody);
  http_response response = client.request(req).get();
  std::cout<<response.status_code();
}

如果我将cmisaction更改为createDocument,则相同的代码可用于上传新文件。
请给我一个解决方案,以通过C ++ rest SDK更新露天文件中的文件内容

The same code works for uploading a new file if I change the cmisaction to createDocument. Please give me a solution to update the contents of a file residing in alfresco throught C++ rest SDK

推荐答案

std::string CMISRESTMETHOD:: UpdateFile(std::string ticket,std::string hostName,std::string portNum,std::string updateFilepath,std::string ClientpathtoDocument,bool ismajorVersion)
{
    try
    {
        if(ticket.empty())
        {
            throw std::runtime_error("Please pass the authentication ticket");
        }
        else if(hostName.empty())
        {
            throw std::runtime_error("Please pass the hostName"); 
        }
        else
        {
        ReplaceStringInPlace(updateFilepath," ","%20");
        Concurrency::streams::basic_istream<unsigned char> fileStream =Concurrency::streams::file_stream<unsigned char>::open_istream(utility::conversions::to_string_t(ClientpathtoDocument)).get();
        // Read file stream to string
        concurrency::streams::stringstreambuf streamBuffer;
        fileStream.read_to_end(streamBuffer).get();
        std::string textFile = move(streamBuffer.collection());
        fileStream.close();
        streamBuffer.close();
        std::string url="http://"+hostName+":"+portNum+"/alfresco/api/-default-/public/cmis/versions/1.1/browser/root";
        string_t objectId=getProperties(updateFilepath,conversions::to_string_t(ticket),"cmis:objectId",url);
        if(!objectId.empty())
        {
            int findPos=objectId.find(';');
            string_t objectID=objectId.substr(1,findPos-1);
            string_t majorVersion=L"false";
            if(ismajorVersion)
            {
                majorVersion=L"true";
            } 
            std::string atomUrl="http://"+hostName+":"+portNum+"/alfresco/api/-default-/public/cmis/versions/1.1/atom";
            web::uri_builder builder(conversions::to_string_t(atomUrl));
            builder.append_path(U("/content/id"));
            builder.append_query(U("alf_ticket"),conversions::to_string_t(ticket));
            builder.append_query(L"id",objectID);
            builder.append_query(L"major",majorVersion);
            http_client client(builder.to_string());
            http_request req;
            req.set_method(methods::PUT);
            req.set_body(textFile);
            req.headers().set_content_length(textFile.length());
            http_response res=client.request(req).get();
            if(res.status_code()!=201 && res.status_code()!=200)
            {
            throw std::runtime_error(getStatusCodeInfo(res.status_code()));
            }
            return getStatusCodeInfo(res.status_code());
        }
        else
        {
            throw std::runtime_error("The requested document property is not available.Could not update the document");
        }
        }
    }
    catch(std::exception &ex)
    {
        throw std::runtime_error(ex.what());
    }
}

这篇关于使用C ++ REST SDK发送HTTP POST请求以更新文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆