Error: source.on is not a function when using "multipart/form-data" in custom app

Hey maker community,

I’m stuck on an issue and I can’t find any useful hints. My custom build app which looks like below is just throwing “source.on is not a function” and stops carrying out the request. (screenshot below as well)

I already figured out that "type": "multipart/form-data" is causing the problem. No further hints than “source.on is not a function” is given by make. The exact same request works in Postman.

I’m running out of ideas. :frowning:

[
{
	"url": "/sync/importmodel",
	"method": "POST",
	"type": "multipart/form-data",
	"qs": {},
	"body": {
		"data": {
				"dataModelType": "{{parameters.dataModelType}}",
				"account": "{{parameters.account}}",
				"title":"{{parameters.title}}",
					"options": {	
						"skipPreview": "{{parameters.skipPreview}}",
						"updateExisting": "{{parameters.updateExisting}}",
						"useOEMAccount": "{{parameters.useOEMAccount}}",
						"createMissingAssetTypes": "{{parameters.createMissingAssetTypes}}",
						"useCustomerLocation": "{{parameters.useCustomerLocation}}"
					}
			},
			"files": {
				"value": "{{parameters.file_data}}",
				"options": {
					"filename": "{{parameters.file_name}}"
				}
		}
	}
}
]

Hi Christoph @digitalyours,

If it’s still relevant. I’ve spent a lot of time trying to fix similar error - docs for multipart/form-data uploads are not comprehensive.

My API requires 3 params:

  • csv (file / buffer)
  • description (string)
  • name (string)

Here is the version which worked for me:

{
	"url": "/URL_SLUG_IS_HIDDEN", //removed url
	"method": "POST",
    "type": "multipart/form-data",
    "body": {	 
	 	"csv": {
			"value": "{{parameters.csvFileData}}",
                	"options": {
				"filename": "{{parameters.csvFileName}}"
			}
		},
		"description":"{{parameters.batchDescription}}",
		"name":"{{parameters.batchName}}"
	},
	
	"headers": {
	// removed auth from here
	},
	"response": {
		"output": "{{body}}"
	}
}

Here are Mappable Parameters:

[
	{
		"name": "csvFileName",
		"type": "text",
		"label": "CSV file name",
		"required": true,
		"semantic": "file:name"
	},
	{
		"name": "csvFileData",
		"type": "buffer",
		"label": "CSV file Data",
		"required": true,
		"semantic": "file:data"
	},
	{
        "name": "batchName",
        "type": "text",
        "label": "Batch Name",
        "required": true
    },
	{
        "name": "batchDescription",
        "type": "text",
        "label": "Batch Description",
        "required": true
    }
]

What I understood (this is a guess but it worked for me) is that in case of multipart/form-data the body TEXT params should be listed in “key”:“value” format, where “key” is the key expected by API and “value” is its value. No structures and curly brackets inside.

For FILES one needs to put “value” and “options” keywords inside the curly brackets.

My hypothesis is that “source.on is not a function” is somehow related to what is expected to be received by the server in case of multipart/form-data (google search shows something on that).

Leaving this message here to save time for someone else - I’ve spent few hours to make this work :slight_smile:

Victor.

Hello @VictorK welcome to the community :wave:

Thank you so so much for taking the time to jump into this topic and share your detailed solution!
Your contribution is super valuable, and I’m sure it’ll be a great resource for someone in the future. Thanks for being so community-minded! :raised_hands:

@digitalyours Do you think you could give the proposed solution a try and see if it does the trick for you? Feel free to keep up posted on the progress :slight_smile:

1 Like

I have reached out to the Enterprise Support and I’m no using a custom IML function to solve the issue. This works well for me.

function stringify(obj) {
	if (!obj) return "";
    return JSON.stringify(obj);
}
3 Likes

Heya @digitalyours :wave:

Just wanted to quickly jump in and say thanks a lot for sharing what you learned with us :pray: Super valuable and much-appreciated stuff :clap:

I encountered the same issue, but the solution that worked for others didn’t work for me.

In my case, I saw the same error, and after searching and debugging for a while, I discovered that the problem was due to not including the filename. The API specs for the platform I built the app for indicated that it wasn’t required, so I initially left it out.

What didn’t work:

"body": {
	"pdf": {
		"value": "{{parameters.pdf}}"
	},
	"param1": "xx",
	"param2": "xx"
}

Later, I added the options object back and tried again, but strangely, it still didn’t work. It was only after I removed the module from my scenario and re-added it that the error message finally disappeared.

What worked:

"body": {
	"pdf": {
		"value": "{{parameters.pdf}}",
		"options": {
			"filename": "{{parameters.file_name}}"
		}
	},
	"param1": "xx",
	"param2": "xx"
}
1 Like