HTTP REST POST MULTIPART

Posted by jsandrea on 08-Apr-2018 20:08

Hi everyone, I would like to share with you this procedure, I was looking for documentation about this topic, but I could not found it, this code send an http rest multipart request to alfresco REST API in order to create a folder and inside of this a file .

this is my code:

USING Progress.Lang.*.
USING OpenEdge.Core.*.
USING OpenEdge.Net.HTTP.*.
USING OpenEdge.Net.URI.
USING Progress.Json.ObjectModel.JsonObject.
USING OpenEdge.Net.MultipartEntity.
USING OpenEdge.Net.MessagePart.
USING OpenEdge.Net.HTTP.IHttpRequest.
USING OpenEdge.Net.HTTP.IHttpResponse.
USING OpenEdge.Net.HTTP.Lib.ClientLibraryBuilder.
USING OpenEdge.Net.MessagePart.
USING OpenEdge.Net.MultipartEntity.

DEFINE VARIABLE oClient AS IHTTPClient NO-UNDO.
DEFINE VARIABLE oURI AS URI NO-UNDO.
DEFINE VARIABLE oRequest AS IHttpRequest NO-UNDO.
DEFINE VARIABLE oResponse AS IHttpResponse NO-UNDO.
DEFINE VARIABLE oResponsejson AS JsonObject NO-UNDO.
DEFINE VARIABLE oMultiEntity AS MultipartEntity NO-UNDO.
DEFINE VARIABLE mData AS MEMPTR NO-UNDO.
DEFINE VARIABLE otransforms AS OpenEdge.Core.Memptr NO-UNDO.
DEFINE VARIABLE oPart AS CLASS MessagePart NO-UNDO.
DEFINE VARIABLE class1 AS CLASS OpenEdge.Core.String.
DEFINE VARIABLE transforms AS LONGCHAR NO-UNDO .

ASSIGN
oMultiEntity = NEW MultipartEntity()
oMultiEntity:Boundary = GUID .

COPY-LOB FROM FILE "c:\temp\CcLicense.jar" TO mData.

/*You need to specify each part of http petition*/
/*PART 1: file*/
ASSIGN
otransforms = NEW OpenEdge.Core.Memptr(mData)
oPart = NEW OpenEdge.Net.MessagePart("application/octet-stream",otransforms)
oPart:ContentId = 'filedata':u.
oPart:Headers:Put(HttpHeaderBuilder:Build('Content-Disposition')
:Value('form-data; name="filedata"; filename="CcLicense.jar"')
:Header).

oMultiEntity:AddPart(oPart).

/*PART 2: destination*/
ASSIGN
transforms = "workspace://SpacesStore/20a4ca91-6807-426f-b2a1-98ac5c71363f"
class1 = NEW OpenEdge.Core.String(transforms)
oPart = NEW OpenEdge.Net.MessagePart()
oPart:Body = class1
oPart:ContentId = 'destinationww':u.
oPart:Headers:Put(HttpHeaderBuilder:Build('Content-Disposition')
:Value('form-data; name="destination"')
:Header).

oMultiEntity:AddPart(oPart).

/*EXECUTION OF HTTP PETITION*/
oClient = ClientBuilder:Build():KeepCookies(CookieJarBuilder:Build():CookieJar):Client.
oURI = NEW URI('http', "172.21.24.14", 10001). /*URL y puerto*/
oURI:Path = '/alfresco/service/api/upload?alf_ticket=TICKET_865d1704302d5798c8e6916c97cc4c468734742d'. /*ruta del servicio*/

oRequest = RequestBuilder:POST(oURI, oMultiEntity)
:ContentType('multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW')
:AcceptJson()
:AcceptFormData()
:Request.


oResponse = ClientBuilder:Build()
:Client:Execute(oRequest).


oResponsejson = CAST(oResponse:entity,JsonObject) .


/*CATH HTTP RESPONSE*/
IF oResponse:StatusCode <> 200 AND oResponse:StatusCode <> 201 THEN
DO:
MESSAGE STRING(oResponsejson:GetJsonObject('status'):GetJsonText('description'))
VIEW-AS ALERT-BOX.

MESSAGE STRING(oResponsejson:GetJsonText('message'))
VIEW-AS ALERT-BOX.
END.

MESSAGE STRING(oResponse:StatusReason)
VIEW-AS ALERT-BOX.

DELETE OBJECT oResponsejson NO-ERROR.
DELETE OBJECT oMultiEntity NO-ERROR.
DELETE OBJECT class1 NO-ERROR.
DELETE OBJECT oURI NO-ERROR.
DELETE OBJECT oPart NO-ERROR.
DELETE OBJECT oRequest NO-ERROR.
DELETE OBJECT oClient NO-ERROR.
SET-SIZE(mData) = 0.

I hope this could be useful to someone.

Jaime Sandrea.

All Replies

This thread is closed