cancel
Showing results for 
Search instead for 
Did you mean: 

Does anyone have an example of a JSON upload file that does not use a HTML form. Need to submit request using HTTPRequest. Trying to upload a file from a ColdFusion application.

bgarbee
Champ in-the-making
Champ in-the-making

I need to integrate a ColdFusion application with Alfresco 5.2.  Using Community edition as proof of concept.  I can create folder and empty text files, but I need to upload a file using a ColdFusion CFHTTP POST request.  No forms.  Any ideas or examples.

8 REPLIES 8

janv
Employee
Employee

Hi Bill,

If you're using the new V1 REST API to create a file node and cannot use multipart/form-data for some reason ... then another option might be to create an empty node first (see JSON example ... with an appropriate filename extension, if known) and then update the binary content

Thanks,

Jan

bgarbee
Champ in-the-making
Champ in-the-making

Jan,

Thanks for your reply. Can you provide a JSON example?  I cannot figure out how to specify the file path of the file to be uploaded.  I'm trying to upload PDF documents. What am I missing.

Is it not possible to upload a new file with JSON without using a HTML form?  The approach of creating an empty file and then updating the file content seems like extra work.

Bill

Bill,

POST .../nodes/.../children accepts:

1. either "application/json" => which can be used to create a node of any type, including folders &/or empty files. The node "id" will be returned and in the case of file/content can be used to upload the binary file via "PUT .../nodes/id" as a separate call.

2. or "multipart/form-data" => which can be used to create & upload the binary file as a single call. The advantage here is that you can also specify additional metadata (node properties).

We do not provide a mechanism to use solely JSON to upload a binary file since this would require extra work on both client-side & server-side to custom encode & decode.

Regards,

Jan

bgarbee
Champ in-the-making
Champ in-the-making

Jan,

I tried to update (PUT) a blank document, but it fails.  Below is the ColdFusion code I'm using along with the response I received.  I know you may not know ColdFusion, but it is pretty self explanatory.

ColdFusion Code:

<cfoutput>Start<br></cfoutput> 
<cfset myPDFBinary = "">
<CFFILE ACTION="ReadBinary" FILE="c:\a\testdocument2.pdf" VARIABLE="myPDFBinary">
<cfset myPDFBase64 = toBase64(myPDFBinary)>

<cfset stFields = { "nodeId": "4c9d02f6-c681-44ae-9289-6f8866fd9b17", "name": "my-file.pdf", "nodeType": "cm:content", "contentBody": "#myPDFBase64#" } >

<cfhttp url="http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?alf_tick..." method="put" result="httpResp" timeout="60">
    <cfhttpparam type="header" name="Content-Type" value="application/json"/>
    <cfhttpparam type="body" value="#serializeJSON(stFields)#"/>
</cfhttp>

<cfdump var="#httpResp#">
<cfoutput>End</cfoutput> --->

The # symbol means to insert content of named variable.

Response:

Start

struct
CharsetUTF-8
ErrorDetail[empty string]
Filecontent{"error":{"errorKey":"framework.exception.UnsupportedResourceOperation","statusCode":405,"briefSummary":"06060003 The operation is unsupported","stackTrace":"For security reasons the stack trace is no longer displayed, but the property is kept for previous versions","descriptionURL":"https://api-explorer.alfresco.com"}}
HeaderHTTP/1.1 405 Method Not Allowed Server: Apache-Coyote/1.1 Content-Type: application/json;charset=UTF-8 Content-Length: 321 Date: Thu, 06 Jul 2017 14:37:47 GMT Connection: close
Mimetypeapplication/json
Responseheader
struct
Connectionclose
Content-Length321
Content-Typeapplication/json;charset=UTF-8
DateThu, 06 Jul 2017 14:37:47 GMT
ExplanationMethod Not Allowed
Http_VersionHTTP/1.1
ServerApache-Coyote/1.1
Status_Code405
Statuscode405 Method Not Allowed
TextYES

End --->

Hi Bill,

In summary, that API method (POST .../nodes/{parentFolderId}/children) expects POST not PUT (hence you're seeing a 405 Method Not Allowed).

In more detail:

To create a node (whether it is of type "cm:content" or otherwise) you *must* use POST, see: Create Node. Please note: if you're using multipart/formdata then you can also use the same API method to create a new version by updating the content using the same file/node name.

Alternatively, to update an existing node of (sub-) type "cm:content" with new binary content, you also have the option to use: Update Node Content.

You may find it helpful to first experiment with these endpoint API methods using a generic REST client, such as Postman &/or "curl" 

Please refer to the tutorial series linked from: Alfresco public REST APIs

In particular: https://community.alfresco.com/community/ecm/blog/2016/10/24/v1-rest-api-part-3-creating-nodes  & https://community.alfresco.com/community/ecm/blog/2016/11/11/v1-rest-api-part-5-versioning-locking 

Regards,

Jan

bgarbee
Champ in-the-making
Champ in-the-making

Jan,

Update Node Content is what I was attempting to do with method = PUT when I received the 405 error.  If I'm reading the API-Explorer correctly, PUT is the correct method to use. I have already reviewed the tutorial series, but I will review again.

Thanks.

Bill

janv
Employee
Employee

Hi Bill,

If you have the node id (usually a uuid format, such as '7c58ebde-226b-4d6e-b6b3-597fb9c4871f') of the content node then you have the option to update it's binary content using:

PUT .../nodes/7c58ebde-226b-4d6e-b6b3-597fb9c4871f/content

 

Regards,

Jan

bgarbee
Champ in-the-making
Champ in-the-making

Jan,

I was able to get the PUT method to work, however, the updated PDF file is corrupted.  The update file includes the "contentBodyUpdate":  parameter in the PDF file.  Below is a snapshot of the beginning of the PDF file.

{"contentBodyUpdate":"JVBERi0xLjUNJeLjz9MNCjE5IDAgb2JqDTw8L0xpbmVhcml6ZWQgMS9MIDMxNDc2L08gMjEvRSAyNjYyMy9OIDEvVCAzMTE3Mi9IIFsgNDYxIDE1M10+Pg1lbmRvYmoNICAgICAgICAgICAgICAgICAgDQoyNyAwIG9iag08PC9EZWNvZGVQYXJtczw8L0NvbHVtbnMgNC9QcmVkaWN0b3IgMTI

Below is the parameters used and the HTTP command.  What is wrong with the coding? 

<cfset stFields = { "nodeId": "82e2f43f-394d-4b03-bea0-b9fb0fa8e284", "name": "my-file.pdf", "nodeType": "cm:content", "contentBodyUpdate": "#myPDFBase64#" }

<cfhttp url="http://127.0.0.1:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/82e2f43f-394d-4b03-bea..." method="put" result="httpResp" timeout="60">
    <cfhttpparam type="header" name="Content-Type" value="application/json"/>
    <cfhttpparam type="body" value="#serializeJSON(stFields)#"/>
</cfhttp>

I also discovered the API-Explorer I downloaded does not display the same information the one you referenced.  For example, the PUT /nodes/{nodeId}/content shows a contentBody parameter, not contentBodyUpdate.  There must be a newer version of the API-Exxplorer.

I'm beginning to believe JSON is not the way to go.

Bill