Creating a smart_collection using curl but getting error: Required parameter missing or invalid

PaulCutcliffe
Tourist
12 0 1

Hi

I'm quite new to the Shopify API and APIs in general, and I'm trying to create a load of smart_collections that includes products whose tags match the collection titles. 

I have this:

curl -d '{"smart_collection":{"title":"Test","rules":[{"column":"tag","relation":"equals","condition":"Test"}]}}' -X POST "https://{my Private app's Admin API key}:{my Private app's Admin API password}@{my Shopify Site}.myshopify.com/admin/api/2021-10/smart_collections.json" -H "X-Shopify-Access-Token: {my Private app's Admin API password}"

 

 

I was unclear at first how to get the access token right so I kept getting this:

{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}

 

 

Now I've got that right but I'm getting this instead, but I can't see that I'm missing any parameters or have any that are invalid:

{"errors":{"smart_collection":"Required parameter missing or invalid"}}

 

 

Can anyone help me to put this right?

Thanks. 

Replies 18 (18)
awwdam
Shopify Staff
175 32 17

Hey @PaulCutcliffe,

Just wanted to confirm if you are using this doc as a guide? Have you tested a GET or PUT request using this API? From the info shared, the only think noticed was that the cURL requests examples from our docs appear to be using slightly different info than the examples your shared:

curl -d '{"smart_collection":{"title":"IPods","rules":[{"column":"title","relation":"starts_with","condition":"iPod"}]}}' \
-X POST "https://your-development-store.myshopify.com/admin/api/2021-10/smart_collections.json" \
-H "X-Shopify-Access-Token: {access_token}



Let me know if you are still running into errors after testing with our exampleformatting. If so, feel free to pass back any x-request-id response headers that are returned with the error in question (these are always safe to share on the forums as they contain no sensitive data). I'd be happy to take a closer look and  share back any insights I can - Cheers!

awwdam | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

PaulCutcliffe
Tourist
12 0 1

Hi

Thanks for coming back to me regarding my issue. 

To answer your question, yes - I looked at this doc and that's how I came up with the Curl commands.

I am using slightly different information from that used in the example - after all, my website doesn't sell iPods! 🤨 But other than catching products based on their tag rather than their title, and using the only relation allowed for tag which is equals, my code is pretty much identical to the example. 

I am still running into errors - here is my output when I run the command:

C:\Users\{UserName}>curl -d '{"smart_collection":{"title":"Accounting","rules":[{"column":"tag","relation":"equals","condition":"Accounting"}]}}' -X POST "https://{API key}:{API Password}@{my-website}.myshopify.com/admin/api/2021-10/smart_collections.json" -H "X-Shopify-Access-Token: {API Password}"
{"errors":{"smart_collection":"Required parameter missing or invalid"}}
C:\Users\{UserName}>

As for testing a GET or PUT request, I have set up Pipedream and sent a simple POST request which returns my shop id and name, and I have re-jigged my request into node.js:

import Shopify, { DataType } from '@shopify/shopify-api';

const client = new Shopify.Clients.Rest('{my-website}.myshopify.com', accessToken);
const data = await client.post({
  path: 'smart_collections',
  data: {"smart_collection":{
    "title":"Anthropology",
    "rules":[
      {
        "column":"tag",
        "relation":"equals",
        "condition":"Anthropology"
        }
      ]
    }
  },
  type: DataType.JSON,
});

But then I get this result:

TypeErrorCannot read property 'Rest' of undefined
at Object.default (file:///opt/ee/c_9OfWxj7/index.mjs:4:36)
at MessagePort.messageHandler (/opt/nodejs/node_modules/@pipedreamhq/execution-environment/launch_worker.js:594:41)
No return values or exportsfor this step

The error from my Curl command says a required parameter is missing or invalid, but I can't find anything in the documentation on what I am missing. 🤯

Thanks again,

Paul Cutcliffe

awwdam
Shopify Staff
175 32 17

Hey @PaulCutcliffe,

Took another look after your reply, tested this request with an API client to confirm, and everything worked fine. So I started comparing for differences between the requests, and noticed a lack of Content-Type header in the cURL request examples from our docs (and used thus far). This header is required for any POST or PUT requests that contain a JSON body.

Appending the following header into a few test cURL requests resulted in success and 0 errors.

-H "Content-Type: application/json"

 
I've made sure to pass this on the necessary teams so it can be updated, and am confident the info above should resolve any further errors of this nature - Best! 

awwdam | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

PaulCutcliffe
Tourist
12 0 1

That's really interesting and encouraging because I stumbled across the same idea and added the very same content-type header so now I have this:

curl -d '{"smart_collection":{"title":"Accounting","rules":[{"column":"tag","relation":"equals","condition":"Accounting"}]}}' -X POST "https://{my-website}.myshopify.com/admin/api/2021-10/smart_collections.json" -H "X-Shopify-Access-Token: {API Password}" -H "Content-Type: application/json"

So now when I run my curl command, I get...

Bad Request

 🤯

awwdam
Shopify Staff
175 32 17

Hey @PaulCutcliffe, there may to be a few backslashes "\" missing from your request, so I wanted to share the body I used: 

curl -d '{"smart_collection":{"title":"IPods","rules":[{"column":"title","relation":"starts_with","condition":"iPod"}]}}' \
-X POST "https://your-development-store.myshopify.com/admin/api/2021-10/smart_collections.json" \
-H "X-Shopify-Access-Token: {access_token}" \
-H "Content-Type: application/json"


If you test this but still get errors please feel free to pass on an x-request-id response header returned with the Bad Request error. I'd be happy to check our logs and see if I can't pass back any other insights! No rush, I'll keep an eye out here. 

awwdam | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

PaulCutcliffe
Tourist
12 0 1

I'm sorry - would you mind showing me exactly what I would need to add to the curl command to pass on an x-request-id response header returned with the Bad Request error? Thanks. 

PaulCutcliffe
Tourist
12 0 1

And as for the backslashes, I don't believe they're part of the code - they just indicate that you should continue the code on the same line. 

If I post a command on more than one line with backslashes between, it just treats each new line as a new command, thereby breaking the previously unfinished command. 

PaulCutcliffe
Tourist
12 0 1

Ah, don't worry - I think I've sussed the X-Request-ID header thing.

I just ran this command:

 

curl -d '{"smart_collection":{"title":"Accounting","rules":[{"column":"tag","relation":"equals","condition":"Accounting"}]}}' -X POST "https://{my-website}.myshopify.com/admin/api/2021-10/smart_collections.json" -H "X-Shopify-Access-Token: {access-token}"  -H "Content-Type: application/json" -H "x-request-id: cutcliffe-smart_collection-issue"

 

... and got the usual Bad Request response, so hopefully, you'll now be able to let me know what was so bad about my request.

Thanks!

awwdam
Shopify Staff
175 32 17

Hey @PaulCutcliffe, there may have been some confusion my last request, and also hadn't realized you were still testing by way of the app. When testing for API errors, I generally use an API client or cURL directly to remove any potential for configuration errors as well to help streamline the troubleshooting process.  

That said, could you share an x-request-id response header that is returned with the Bad Request error here in this forum thread - this header wouldn't be added to a request rather returned in a response, providing us a quick way to check logs on our end.

No rush again and happy to keep helping!


awwdam | API Support @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog