Multipart/form-data with JSON

Hi,
im my custom app, I am trying to submit the following request, but it seems to be impossible:

POST <BASE-URL>/documents
Content-Type: multipart/form-data; boundary=<BOUNDARY> 
Transfer-Encoding: chunked
--<BOUNDARY>
Content-Disposition: form-data; name=metadata 
Content-Type: application/json
{
  "metadata":  {
   "document" :  {
        "filename" : <filename>,
        "title" : <title>
     }
  }
}
--<BOUNDARY>
Content-Disposition: form-data; name=document; filename=<filename> 
Content-Type: <mime-type>
<File contents> 
--<BOUNDARY>--

The API documentation says:

Note that both body parts are required and that their order is fixed: The “metadata” body part comes first, and the “document” body part comes second. Upload requests missing a part, or that contain parts in the wrong order, will not succeed.

I tried this request:

{
	"url": "/documents",
	"method": "POST",
	"type": "multipart/form-data",
	"headers": {
		"Content-Type": "multipart/form-data"
	},
	"qs": { },
	"body": {
        "metadata": {
            "value": "{}",
            "options": {
				"type": "application/json"
            }
        },
        "document": {
            "value": "{{parameters.file}}",
            "options": {
                "filename": "{{parameters.filename}}"
            }
        }
	},
	"response": {
		"output": "{{body}}"
	}
}

but the server error message is: Bodypart metadata is not of type application/json

Is it possible to set the content type for the json part?

Best Regards
Jörg

1 Like

I’m having the same issue, it seems that only the JSON body is available through the API call. I really hope they fix it!

@cliff.design

I have worked prior on a similar setup where a file needs to be uploaded alongside other formdata in Custom App.

Can you share me the API so I can review it and see how I can help you on this?

1 Like

@Runcorn

I’m using the Harvest App Expenses API, I can send all the data except for the file receipt! I’d be interested to see your solution to Patch or Post these files within Make.

@Jorg_Ackermann @cliff.design Did you have any luck with that request?

I was trying to do a similar thing right now with Salesforce Upload ContentVersion (using Make API call as Make API ContentVersion implementation appears buggy).

--boundary_string
Content-Disposition: form-data; name="entity_content";
Content-Type: application/json

{
    "ContentDocumentId" : "",
    "ReasonForChange" : "First upload",
    "PathOnClient" : "{{17.`Lead Name`}}.zip",
    "Title": "{{17.`Lead Name`}}.zip",
     "OwnerId": "XXXXXX",
     "FirstPublishLocationId":"{{22.Id}}"
}

--boundary_string
Content-Disposition: form-data; name="VersionData"; filename="{{17.`Lead Name`}}.zip"
Content-Type: application/octet-stream; UTF-8

{{6.fileOutput}}

--boundary_string--

The result is that I am upload the the file successfully, but content is gibberish. My guess is that Make serializes somehow the Input field and the binary input stops being binary. I think I have used once the native Make HTTP and use Multipart/form as Content-type which gives nice field for the file and that should work, but that also means building Salesforce Oauth from scratch.

Any thoughts if above could work?

Hey @Jorg_Ackermann,

according to the specification of multipart/form-data request, you need to send the value as buffer. So in your case, you would need to use the IML function toBinary(). So the your request would look like this:

...
"body": {
        "metadata": {
            "value": "{{toBinary('{}')}}",
            "options": {
				"type": "application/json"
            }
        },
        "document": {
            "value": "{{parameters.file}}",
            "options": {
                "filename": "{{parameters.filename}}"
            }
        }
	},
...