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.

0 Likes
Jason
Shopify Expert
10273 144 1943

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 to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
0 Likes
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.

0 Likes
Jason
Shopify Expert
10273 144 1943

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 to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
0 Likes
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.

0 Likes