Can we use encoded URLs to fetch data from Ghost API


#1

Hi,

Is there any way that we can fetch data from encoded URLs from Ghost API?

https://xxxxxxx.xx/ghost/api/v0.1/posts/?client_id=ghost-frontend&client_secret=xxxxx&formats=plaintext&limit=30&filter=tags:-[techroom,guides],published_at:%3C%272018-12-30T11:16:34.000Z %27

published_at:<'2018-12-30T11:16:34.000Z' => published_at:%3C%272018-12-30T11:16:34.000Z %27


#2

Hey!

You can use the new Content API :relaxed: https://docs.ghost.org/api/content/


#3

Thank you for the quick reply.

It’s glad to know that the v2 API is finally stable. But my issue will persist since I have to query published_at:>'2017-06-03 23:43:12' with special characters, isn’t it?

:thinking:


#4

@truekasun The query parameters MUST be url encoded in order for the query to work.

If you use our Client Libraries (old or new) this is all done for you.

But maybe we haven’t understood the question…


#5

@Hannah I am using a library called Retrofit for Android to fetch data from Ghost API.
But when I make a request to the Ghost API with filter like published_at:<'2018-12-30T11:16:34.000Z' the special characters are automatically encoded like this published_at:%3C%272018-12-30T11:16:34.000Z %27.

I know this is not a problem with the Ghost API. I’ve searched a way to escape from that conversion done by the particular library but no luck with that. I’m just wondering if I can fetch data with a URL like this, from Ghost API.

https://xxxxx.xx/ghost/api/v0.1/posts/?client_id=ghost-frontend&client_secret=xxxxxx&formats=plaintext&limit=30&filter=tags:-[techroom,guides],published_at:%3C%272018-12-30T11:16:34.000Z%27

You can see in the filters, the published_at section that special characters are encoded.


#6

@truekasun that is a perfectly valid URL, are you having trouble with it? What errors are you getting?


#7

It’s not just valid, it’s required. You cannot make a request to any of the Ghost APIs without the encoding as the URL would be invalid and you would definitely get an error back from the filter.

I strongly suggest switching to the v2 API as well. It’s NOT a big change - it’s mostly just swapping the client_id and client_secret for a single content API key. However, we’ve done tonnes of cleanup on the responses and it’ll help you to not accidentally depend on features that are about to disappear.


#8

@fabien @Kevin @Hannah I’m sorry that I was in a misunderstanding it wasn’t about the encoding. It was about the multiple filter combinations (+ and operator).

As per your advice, I have upgraded to the Ghost content API v2. Please refer the below URL

https://xxx.xxx/ghost/api/v2/content/posts/?key=xxxxxx&fields=uuid%2Ctitle%2Cfeature_image%2Cslug%2Cpublished_at&limit=30&filter=published_at:%3C%272018-12-25T20:30:44.000+05:30%27+tags:-[techroom,guides]

Expected result: 30 posts which are NOT tagged as techroom or guides that published before the given date.

(I will change the API key after you guys take a look)

If I use (or | ,) for multiple filters it works. But when I try to use (and | +) it throws me the following error.
chrome_2019-01-14_23-29-00

Ghost version: 2.10.1

And I would like to take a chance to give my pleasure to the Ghost team. The v2 API is the best ever REST API I’ve used in my career. It is just awesome and I have no words to express the feeling I’m having right now. Thank you again! :blush:


#9

Ok, let’s go back to the beginning.

The raw, unencoded filter you want to apply is: published_at:<'2018-12-25T20:30:44.000+05:30'+tags:-[techroom,guides]

The correctly encoded version of this is published_at%3A%3C'2018-12-25T20%3A30%3A44.000%2B05%3A30'%2Btags%3A-%5Btechroom%2Cguides%5D.

This works:

https://technews.lk/ghost/api/v2/content/posts/?key=05813b87be780ed3aa9a2bbdec&fields=uuid%2Ctitle%2Cfeature_image%2Cslug%2Cpublished_at&limit=30&filter=published_at%3A<'2018-12-25T20%3A30%3A44.000%2B05%3A30'%2Btags%3A-[techroom%2Cguides]

I don’t know what the Android way to do this kind of encoding is, but it’s easy to do manually by opening up the console in your browser and calling encodeURIComponent("published_at:<'2018-12-25T20:30:44.000+05:30'+tags:-[techroom,guides]").


#10

@Hannah Thank you very much. The whole point is, I haven’t encoded the + sign from the beginning. My bad. :expressionless: Everything works like a charm. Thank you very much again!