Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Issue trying to create metafield for product

Issue trying to create metafield for product

DSL
Tourist
6 0 3

Reference:

https://shopify.dev/docs/admin-api/rest/reference/metafield

 

I am trying to add a metafield for a product by making a REST call using format:

 

POST https://[KEY]:[PASS]@[SHOP_NAME].myshopify.com/admin/api/2020-04/products/[PRODUCT_ID}/metafields.json

with body:

 

{
   "metafield":
   {
         "namespace": "resource",
         "value_type": "string",
         "value": "my custom value",
         "key": "my_custom_key",
         "description": "The description."
    }
}

But I receiving a response with status 400 like:

{

    "size":0,

   "timeout":0

}

What am I doing wrong?

Replies 5 (5)

SBD_
Shopify Staff
1831 273 421

Hey @DSL 

 

400 indicates a bad request. How are you sending the request? Is the Content-Type application/json?

Scott | Developer Advocate @ Shopify 

DSL
Tourist
6 0 3

Yes, content-type is set to application/json

SBD_
Shopify Staff
1831 273 421

Are you able to capture a request ID from the response headers?

Scott | Developer Advocate @ Shopify 

robertjla
Tourist
3 0 1

Has a solution ever been provided? I too cannot create a metafield for a product using the admin api. I did the following:
URL: https://{API Key}:{Password}@{MyStore}.myshopify.com/admin/products/{product-id}/metafields.json
Action: Post
Body:

{
    "metafield": {
        "namespace""global",
        "key""Test_Metafield",
        "value""Value for test metafield",
        "value_type""string",
        "description""This is the description"
    }
}
 
When I try sending it I get a status 200 but with HTML as the response but no metafield was created for the product:
 
<html>

<body>
    <noscript>
        <a
    </noscript>

    <script type="text/javascript" defer>
        window.location = "https:\/\/accounts.shopify.com\/oauth\/authorize?client_id=xxxxxxxxxxxxxxxxxxx\u0026destination_uuid=4xxxxxxxxxxxxxxxxxxxx\u0026nonce=xxxxxxxxxxxxxxxxxxx\u0026prompt=merge\u0026redirect_uri=https%3A%2F%2Fxxxxxxx.myshopify.com%2Fadmin%2Fauth%2Fidentity%2Fcallback\u0026response_type=code\u0026scope=email%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fdestinations.readonly%20openid%20profile%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fpartners.collaborator-relationships.readonly%20https%3A%2F%2Fapi.shopify.com%2Fauth%2Fbanking.manage\u0026state=xxxxxxxxxxxxxxxxx\u0026ui_locales=en\u0026ux=shop";
    </script>
</body>

</html>


I even tried using basic authentication with the username set to my API key and the password with my API password and removed those values from the URL but I still get the same response in which no metafield gets created.

Jason
Shopify Partner
11206 226 2315

@robertjla - I've replied on your other thread.
https://community.shopify.com/c/Shopify-APIs-SDKs/Can-t-Perform-POST-using-REST-API-and-Basic-Authen...

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★