Adding Metafields

msaid
Tourist
3 0 0

Hi there. I am trying to add a metafield to a product. I am expecting that the new metafield to be available in the selection table below.

When I submit the request I get status 200 and the the following response body:

 

 

<html>
<body>
    <noscript>
        <a
            href="https://accounts.shopify.com/oauth/authorize?client_id={OMITTED}
    </noscript>
    <script type="text/javascript" defer>
        window.location = "https:\/\/accounts.shopify.com\/oauth\/authorize?client_id={OMITTED};
    </script>
</body>
</html>

 

 

When I check the product bulk editor there are no new metafields.  

 

Steps taken:

I am using postman to make the post request with the following URL:

https://{MY STORE}/admin/api/2021-04/products/{PRODUCT ID}/metafields.json

The request body is:

 

 

{
  "metafield": {
    "namespace": “test”,
    "key": “test_metafield”,
    "value": 50,
    "value_type": "integer"
  }
}

 

 

Authorisation is set to “basic auth” and I have used my username and password for the private app.

I think I missing something fundamental here. Am I using the API incorrectly or am I more generally thinking about the metafields feature incorrectly? 

I would like to understand how manipulating metafields works without using an app.

Any and all help greatly appreciated.

Replies 5 (5)

Jason
Shopify Expert
11190 225 2282

So you're trying to make a metafield in Postman and you're getting that response back? In the desktop version of postman look to the top right hand side for the "cookies" option. Click on that and remove all the cookies.

Change requests (like POST) made with cookies attached are blocked for for good security reasons.

★ 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 ★
msaid
Tourist
3 0 0

Thanks Jason. No luck unfortunately. I had deleted cookies previously after seeing one of your responses to an earlier post - but I double checked just in case.

I've since tried sending a fetch request from the store console as follows:

fetch('https://{MY STORE}.myshopify.com/admin/api/2021-04/products/{PRODUCT ID}/metafields.json', {
    method: 'post',
    headers: {
      "Content-type": "application/json; charset=UTF-8"
    },
    body: JSON.stringify({
      metafield: {
        namespace: "cars",
        key: "warehouse",
        value: 25,
        value_type: "integer"
      }
})
  })
  .then( (response) => { 
    console.log(response)
});

I still get a successful 200 response but no change in the metafields.  I ran a get request on the products as well to check which returns an empty metafields array.

Any thoughts? Any other info I could provide which might help diagnose the problem?

With thanks.

Jason
Shopify Expert
11190 225 2282

It won't work from the browser console given cookies are always sent in those. If you're still having issues then capture the request id that's in the header response and post that here. It still feels like authentication issues to me on first glance but let's see.

★ 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 ★
msaid
Tourist
3 0 0

Many thanks Jason.

I tried running the same request through application.js (and separately within a script tag) with the same result (not sure if that would make a difference anyway given the request will still originate in the browser). As a guess I tried embedding the script and request within liquid and had the same result again.

In all attempts the request is returning a status code of 301 and, for a request from application.js, the response header returns cf-request-id: 09f063162c0000e9b7502fa000000001.

Let me know if I can provide further information or run a different test to help diagnose the problem.

My thanks again for your help with this.

dstefani
Shopify Partner
13 0 0

For future reference, this is what I used:

[POST]

https://{{api_key}}:{{api_password}}@{{store_name}}.myshopify.com/admin/api/{{api_version}}/products/6609497620686/metafields.json

Cleared cookies as mentioned above, then

{
    "metafield": {
        "namespace": "SEO",
        "key": "google_product_category",
        "value": "All Flowers > Sorbet Rose",
        "value_type": "string",
        "description": "a meta field for product feed"
    }
}

 

Worked just fine, to test it's success, use the above URL as a [GET] and you should see your newly added metafield

 

- D