Update a variant by method 'put'

Solved
Tourist
3 1 0

Hi

I would update a variant by PUT method.

here is my request :

 

(
	[method] => PUT
	[url] => https://[API key]:[Password]@mysite.myshopify.com/admin/variants/31278544338.json
	[data] => {"id":31278544338,"image_id":10763378130}
)

But I have a this error :

 

 

[http_status] => 400 Bad Request -> The request was not understood by the server, generally due to bad syntax.
[response] => Array
	(
		[errors] => Array
			(
				[variant] => Required parameter missing or invalid
			)

	)

However my URL it is correct : https://[API key]:[Password]@mysite.myshopify.com/admin/variants/31278544338.json
I can see on my navigator.

 

 

Here is my code

$variantID = 31278544338;

$inventoryData = [
	'id' => $variantID,
	'image_id' => 10763378130,
];

$shopifyResult = $shopifyAPI->makeRequest('PUT', "variants/{$variantID}.json", $inventoryData);

and here is my Function : makeRequest :

makeRequest($method, $endpoint, $args = []){

//... etc.  ...

$requestURL = $this->apiURL . $endpoint; 
$json_data = json_encode($args);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

switch ($method) {
	case 'POST':
	case 'PUT':
		curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
		break;
}
$shopifyResponse = curl_exec($ch);
$executionInfo = curl_getinfo($ch);
//... etc.  ...

Do you know where I made some mistake ?

Thnaks

 

0 Likes
Highlighted
Tourist
3 1 0

Hi,
I tried also with this new code but it doesn't work.

here is my code :

makeRequest($args = []){

$json_data = json_encode($args);

$url = "https://[API key]:[Password]@mysite.myshopify.com/admin/variants/31278544338.json";
$ch = curl_init( $url );
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($args)); 
curl_setopt($chCURLOPT_SSL_VERIFYPEER$sslVerification); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $shopifyResponse = curl_exec($ch); $executionInfo = curl_getinfo($ch); var_dump(json_decode($shopifyResponse, TRUE)); //... etc. ... } $variantID = '31278544338'; $inventoryData = [ 'id' => $variantID, 'image_id' => '10763378130', ]; $shopifyResult = $shopifyAPI->makeRequest($inventoryData);

Here is a same response by server :

(
    [success] => 
    [request] => Array
        (
            [method] => PUT
            [url] => https://[API key]:[Password]@mysite.myshopify.com/admin/variants/31278544338.json
            [data] => {"id":"31278544338","image_id":"10763378130"}
        )

    [http_status] => 400 Bad Request -> The request was not understood by the server, generally due to bad syntax.
    [response] => Array
        (
            [errors] => Array
                (
                    [variant] => Required parameter missing or invalid
                )

        )

)

Could you help me please.
Thanks

0 Likes
Highlighted
Shopify Staff
Shopify Staff
297 32 63

Hey there,

 

I believe you're running into this 400 error because you are using an invalid image id.

 

The variant PUT request allows you to add an existing image to an existing variant, however, I don't see any existing images with id 10763378130.

 

I see the associated product has two images with ids 19080633682 and 19080633682. 

 

Are you sure you didn't mean to use one of those values instead?

0 Likes
Highlighted

Success.

Tourist
3 1 0

Hi Zameer,

You right!

But my error come from "$inventoryData "

$inventoryData = [
	'id' => $variantID,
	'image_id' => '10763378130',
];

it should be: 

$inventoryData = [
	'variant' => [
		'id' => $variantID,
		'image_id' => '10763378130',
	],
];

I forgot added 'variant' array.

So know it works

Thanks

0 Likes