BIM Coordinator Program (INT) April 22, 2024
Find the next step in your career as a Graphisoft Certified BIM Coordinator!
Archicad C++ API
About Archicad add-on development using the C++ API.

Send file in multipart-form POST request

Ankur Yadav
Participant

I want to send a .xlsx file to the server using Restful POST request in a multipart-form data. I am able to send normal text in POST body, but how do i send a file. I am using Communication::Client::Http::HttpClientRequest as shown in CommunicationClient_test example in SDK.

1 REPLY 1
roni
Booster

Hi Ankur, 
I was able to get response from the call with status 200, but not able to parse the response using the code below

using namespace HTTP::Client;
	using namespace HTTP::MessageHeader;
	using namespace GS::IBinaryChannelUtilities;

	IO::URI::URI connectionUrl ("https://identity.thenbs.com/");
	ClientConnection clientConnection (connectionUrl);
	clientConnection.Connect ();

	Request postRequest (Method::Post, "/core/connect/token");

	GS::UniString clientID = "user";
	GS::UniString clientSecret = "pass";
	// More information on the API, and on getting an API key:
	// https://toolkit.thenbs.com/articles/for-software-developers/#bimToolkitAPI

	RequestHeaderFieldCollection& headers = postRequest.GetRequestHeaderFieldCollection ();
	Authentication::BasicAuthentication auth (clientID, clientSecret);
	auth.HandleRequestHeaderFieldCollection (headers);
	headers.Add (HeaderFieldName::ContentType, "application/x-www-form-urlencoded");

	GS::UniString postBody = "scope=bimtoolkitapi&grant_type=client_credentials";
	clientConnection.Send (postRequest, postBody.ToCStr (), postBody.GetLength ());

	Response response;
	JSON::JDOMParser parser;
	JSON::ValueRef parsed = parser.Parse (clientConnection.BeginReceive (response));

	DBPrintf ("HTTP %u\n", response.GetStatusCode ());
	if (response.GetStatusCode () == StatusCode::OK) {
		JSON::ObjectValueRef object = GS::DynamicCast<JSON::ObjectValue> (parsed);
		JSON::StringValueRef tokenValue = GS::DynamicCast<JSON::StringValue> (object->Get ("access_token"));
		if (tokenValue != nullptr) {
			GS::UniString token = tokenValue->Get ();
			DBPrintf ("NBS Token: %s\n", token.ToCStr ().Get ());
		}
	}

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


Could you help on this? 

Learn and get certified!