Filter directly on api call


#1

Hi
can you do a filter directly on the api call?

for example:

brandName() {
    return this.http.get(
      `${this.uri}products?filter=eq(brands, brandName)`,
      { headers: new HttpHeaders().set('Authorization', 'Bearer ' + localStorage.getItem('auth')) }
    );
  }

#2

@handre It’s currently only possible to filter products by brand use the id of the desired brand record. See API reference.

api.moltin.com/v2/products?filter=eq(brand.id, 'id')

#3

hi @jonathan

must it be in quotation? For some reason I am strugeling to break out of the quotes directly in my api call.

\' xxx \' still getting a %27xxx%27 in my response.


#4

Hi @jonathan

I still am not able to filter directly through the api call?

filorga() {
    return this.http.get(
      `${this.uri}products?filter=eq(brand.id,' 309d4051-6b0e-47d2-afad-eeb07a696084')`,
      { headers: new HttpHeaders().set('Authorization', 'Bearer ' + localStorage.getItem('auth')) }
    );
  }```

#5

@handre I’m sorry, I messed up the API call. This will do it:

https://api.moltin.com/v2/products?filter=eq(brand.id,id)

No space between the parameter (brand.id) and the resource ID and no quotation marks around the ID.


#6

Hi @jonathan

I am getting the following error:

Cross-Origin Read Blocking (CORB) blocked cross-origin response https://api.moltin.com/products?filter=eq(brand.id,309d4051-6b0e-47d2-afad-eeb07a696084) with MIME type application/json


#7

Hi @handre

How do you have your project set up? Are you serving from the filesystem or a web server, local or otherwise?


#8

Hi @drew

I am using my local server at the moment.


#9

Hi @handre if possible can you show me a screenshot of the call being sent from the network tab in the dev console?


#10


#11

Hi @handre

On this line
${this.uri}products?filter=eq(brand.id,' 309d4051-6b0e-47d2-afad-eeb07a696084')
you have a space before the id.

If you format the query like:

${this.uri}products?filter=eq(brand.id,'309d4051-6b0e-47d2-afad-eeb07a696084')}

This should work and return an Access-Control-Allow-Origin header


#12

hi @drew

according to @jonathan?


#13

That is correct, I have tested the call in Postman against my test store and can see that access-control-allow-origin is returned in the header.

The response body should look something like this:

{
    "data": [
        {
            "type": "product",
            "id": "0cf771e3-e430-43e1-8f87-fea9f553635b",
            "name": "A Product",
            "slug": "main-product",
            "sku": "main",
            "manage_stock": true,
            "description": "The main product in the related items flow",
            "price": [
                {
                    "amount": 10,
                    "currency": "USD",
                    "includes_tax": true
                }
            ],
            "status": "live",
            "commodity_type": "physical",
            "meta": {
                "timestamps": {
                    "created_at": "2018-09-03T13:58:10+00:00",
                    "updated_at": "2018-09-20T11:25:34+00:00"
                },
                "display_price": {
                    "with_tax": {
                        "amount": 10,
                        "currency": "USD",
                        "formatted": "$0.10"
                    },
                    "without_tax": {
                        "amount": 10,
                        "currency": "USD",
                        "formatted": "$0.10"
                    }
                },
                "stock": {
                    "level": 10,
                    "availability": "in-stock"
                }
            },
            "relationships": {
                "related-items": {
                    "data": null
                },
                "brands": {
                    "data": [
                        {
                            "type": "brand",
                            "id": "9e59b851-02ec-4af4-82ea-bcd502787d09"
                        }
                    ]
                }
            }
        }
    ],
    "links": {
        "current": "https://api.moltin.com/v2/products?page[limit]=100&page[offset]=0&filter=eq(brand.id,9e59b851-02ec-4af4-82ea-bcd502787d09)",
        "first": "https://api.moltin.com/v2/products?page[limit]=100&page[offset]=0&filter=eq(brand.id,9e59b851-02ec-4af4-82ea-bcd502787d09)",
        "last": null
    },
    "meta": {
        "results": {
            "total": 1,
            "all": 1
        },
        "page": {
            "limit": 100,
            "offset": 0,
            "current": 1,
            "total": 1
        }
    }
}

Hope this helps


#14

@handre just spotted that your URL also is missing /v2 in the call. The URL should be https://api.moltin.com/v2/products


#15

hi @drew

It looks like that was my error.