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

PaulCutcliffe
Shopify Partner
13 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 19 (19)

awwdam
Shopify Staff
249 42 36

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
Shopify Partner
13 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
249 42 36

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
Shopify Partner
13 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
249 42 36

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
Shopify Partner
13 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
Shopify Partner
13 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
Shopify Partner
13 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
249 42 36

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

PaulCutcliffe
Shopify Partner
13 0 1

Yes, thanks - I understood and did that in my last post, and I used this x-request-id:

-H "x-request-id: cutcliffe-smart_collection-issue"

 I look forward to hearing what's wrong - thanks. 

awwdam
Shopify Staff
249 42 36

Hey @PaulCutcliffe,

In this case the example you shared would be a request header, but we would need the response header value as returned from our server response with the error payload. 


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
Shopify Partner
13 0 1

Aha, and there was me thinking I'd understood what you'd asked of me! 🙄

So I'm guessing your server's response with the error payload is not something that is provided in response to a curl command, so I need to get it working in Pipedream or something like that after all.

PaulCutcliffe
Shopify Partner
13 0 1

Hi

I'm sorry, I still have no idea what you're asking me to do.

Could you explain exactly what I need to do with my curl command so that you can see the Response Header that you need to help me?

Thanks.

Paul Cutcliffe


My Command:

 

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

 

Response:

Bad Request

 

csam
Shopify Staff (Retired)
267 40 51

Hi @PaulCutcliffe 

Try putting the verbose flag -v in your curl command to see the full response.

Thanks!

To learn more visit the Shopify Help Center or the Community Blog.

PaulCutcliffe
Shopify Partner
13 0 1

Ooh, thanks - that gives me a great deal more to go on. 

I shall go through the response and see what I can find. 

PaulCutcliffe
Shopify Partner
13 0 1

Hi

Well, I've certainly got more information now, although I have to confess it doesn't mean much to me - here's the full transcript (with anything sensitive removed):

 

C:\Users\{Username}>curl -v -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" -H "x-request-id: cutcliffe-smart_collection-issue"
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 23.227.38.74...
* TCP_NODELAY set
* Connected to {my-website}.myshopify.com (23.227.38.74) port 443 (#0)
* schannel: SSL/TLS connection with {my-website}.myshopify.com port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 199 bytes...
* schannel: sent initial handshake data: sent 199 bytes
* schannel: SSL/TLS connection with {my-website}.myshopify.com port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with {my-website}.myshopify.com port 443 (step 2/3)
* schannel: encrypted data got 2846
* schannel: encrypted data buffer: offset 2846 length 4096
* schannel: sending next handshake data: sending 93 bytes...
* schannel: SSL/TLS connection with {my-website}.myshopify.com port 443 (step 2/3)
* schannel: encrypted data got 258
* schannel: encrypted data buffer: offset 258 length 4096
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with {my-website}.myshopify.com port 443 (step 3/3)
* schannel: stored credential handle in session cache
> POST /admin/api/2021-10/smart_collections.json HTTP/1.1
> Host: {my-website}.myshopify.com
> User-Agent: curl/7.55.1
> Accept: */*
> X-Shopify-Access-Token: {API-Password}
> Content-Type: application/json
> x-request-id: cutcliffe-smart_collection-issue
> Content-Length: 97
>
* upload completely sent off: 97 out of 97 bytes
* schannel: client wants to read 102400 bytes
* schannel: encdata_buffer resized 103424
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: encrypted data got 876
* schannel: encrypted data buffer: offset 876 length 103424
* schannel: decrypted data length: 813
* schannel: decrypted data added: 813
* schannel: decrypted data cached: offset 813 length 102400
* schannel: encrypted data length: 34
* schannel: encrypted data cached: offset 34 length 103424
* schannel: decrypted data length: 5
* schannel: decrypted data added: 5
* schannel: decrypted data cached: offset 818 length 102400
* schannel: encrypted data buffer: offset 0 length 103424
* schannel: decrypted data buffer: offset 818 length 102400
* schannel: schannel_recv cleanup
* schannel: decrypted data returned 818
* schannel: decrypted data buffer: offset 0 length 102400
< HTTP/1.1 400 Bad Request
< Date: Wed, 10 Nov 2021 14:47:05 GMT
< Content-Type: text/plain
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Sorting-Hat-PodId: 162
< X-Sorting-Hat-ShopId: 40312733859
< X-Shopify-Stage: production
< Content-Security-Policy: frame-ancestors 'none'
< X-Content-Type-Options: nosniff
< X-Download-Options: noopen
< X-Permitted-Cross-Domain-Policies: none
< X-XSS-Protection: 1; mode=block
< X-Dc: gcp-europe-west1,gcp-us-east1,gcp-us-east1
< X-Request-ID: 12772b2f-419b-4ceb-ab7c-6827584ab785
< CF-Cache-Status: DYNAMIC
< Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< Server: cloudflare
< CF-RAY: 6ac012ef89e675a9-LHR
< alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400, h3-28=":443"; ma=86400, h3-27=":443"; ma=86400
<
Bad Request* Connection #0 to host {my-website}.myshopify.com left intact

C:\Users\{Username}>

 

Is anyone able to assist with working out why this command fails?

It mentions that I don't need the -x switch as POST is already inferred, although it doesn't work if I take that switch out as it says there's no URL. There's also something about a handshake failing, and needing more data, but I've got no idea what it means, and I think my command is very similar to the example in the documentation, or at least it will be once you add the requirement for the -H "Content-Type: application/json" header.

Thanks,

Paul Cutcliffe

csam
Shopify Staff (Retired)
267 40 51

Hi @PaulCutcliffe 

I'm not sure what's causing the issue - maybe the x-request-id in your request. That's not a necessary request field. If you leave it out, do you get a 200? I'm not sure what would cause the handshake to take so long either. Are you on a proxied connection? If you use an API client (eg. Postman or Insomnia) are you able to make a successful request? I'm wondering if there is maybe something in your curl config that's causing the issue.

Regards,

To learn more visit the Shopify Help Center or the Community Blog.

PaulCutcliffe
Shopify Partner
13 0 1

Thanks for your response, but I'm afraid removing the x-request-id made absolutely no difference other than that it wasn't mentioned in the response, but every single other line was identical to when I included it. 

This is most frustrating - I need Excel to be able to create a load of Smart Collections automatically, and shelling a curl command seems so very much simpler than sending an HTTP Post. 🤨

PaulCutcliffe
Shopify Partner
13 0 1

@csam , @awwdam 

I never did manage to resolve this issue but I can't help thinking I'm close to having it working and it would sure make what I'm trying to do for my customers SO much simpler as it can all be automated in Excel by shelling the Curl commands out. 

Is anyone able to help me troubleshoot my Curl issue?

 

I just need to be able to create Smart_collections from a command line but can't get past that error message. 

 

Thanks,

Paul Cutcliffe