Archicad C++ API
About Archicad add-on development using the C++ API.

How to post file using api.

Chris Howell
Contributor

Hello everyone,

I am trying to upload file as multipart/form-data using clientconnection. But it failed.

This is my code.

 

void ExportIFC::ExportData(const char* buffer, UINT32 size)
{
using namespace HTTP::Client;
using namespace HTTP::MessageHeader;
using namespace HTTP::Encoding;

IO::URI::URI connectionUrl(LINKURL);
ClientConnection clientConnection(connectionUrl);
clientConnection.Connect();
GS::UniString url = "/upload/" + this->m_projectId + "/" + this->m_uploadHandle;
Request postRequest(Method::Post, url);

RequestHeaderFieldCollection& headers = postRequest.GetRequestHeaderFieldCollection();
GS::UniString bearer = "bearer " + this->m_token;
headers.Add(HeaderFieldName::Authorization, bearer);
headers.Add(HeaderFieldName::ContentType, "multipart/form-data");
headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"test.ifc\"");

BodyOBinaryChannel& outChannel = clientConnection.BeginSend(postRequest);
outChannel.Write(buffer, size);

Response response;
clientConnection.Receive(response);
StatusCode::Id code = response.GetStatusCode();
if (code == StatusCode::OK)
{
 // post process
}
}

clientConnection.FinishReceive();
clientConnection.Close(false);
}

 

What is my fault? Please help me.

 

4 REPLIES 4
dushyant
Enthusiast

Don't you need a clientConnection.FinishSend(); ?

BrunoValads
Contributor

Hey! Was you able to achieve this? I'm trying it too, with clientConnection.FinishSend(), but I'm getting some errors from the server, maybe due how I'm writing. Shouldn't boundaries be written?

Hi,
I could not to upload the file with clientconnection.
So I solved the problem using cURL.

Nice! I solved as well, but using libcpr (a curl wrapper that is very easy to use)