New Shopify Certification now available: Liquid Storefronts for Theme Developers

Upload JSONL File POST - MalFormedPOSTRequest for BulkOperations

Solved
stats_marketing
Shopify Partner
24 3 9

Hi Community,

 

I am trying to upload a JSONL file to update inventory following the docs here: https://shopify.dev/api/usage/bulk-operations/imports#upload-the-jsonl-file

 

I am stuck at the POST request when utilizing CURL and keep getting an error back.

 

Error Message: The body of your POST request is not well-formed multipart/form-data.

 

<error>
<code>MalformedPOSTRequest</code>
<message>The body of your POST request is not well-formed multipart/form-data.</message>
<requestid>YAZV7CVGDH7AJRF4</requestid>
<hostid>z6UUiS7XfntItbFTjqiNcLqKoSDggk76XcsU7WRpehIkqTxTJjBAu3jP7czGNw+Y06tdblv+Smc=</hostid> </error>

I am utilizing curl in PHP:

 

 

$mutation = graphql($access_token, $shop_url, $query);
$decoded_json = json_decode($mutation['body'], false);

$url = $decoded_json->data->stagedUploadsCreate->stagedTargets[0]->url;
$params = $decoded_json->data->stagedUploadsCreate->stagedTargets[0]->parameters;

$json = array();
foreach($params as $p){
	$name = $p->name;
	$value = $p->value;
	
	$json[$name] = $value;

}
$json['file'] = 'test.jsonl';
$fields = json_encode($json, TRUE);
$headers = [
    'Content-Type: multipart/form-data'
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
var_dump($result);

 

 

Accepted Solution (1)
stats_marketing
Shopify Partner
24 3 9

This is an accepted solution.

I found out that everything was correct except I was sending the `$fields` as encoded json when they were in-fact already encoded.

 

Sending the `$fields` variable  without encoding worked.

View solution in original post

Reply 1 (1)
stats_marketing
Shopify Partner
24 3 9

This is an accepted solution.

I found out that everything was correct except I was sending the `$fields` as encoded json when they were in-fact already encoded.

 

Sending the `$fields` variable  without encoding worked.