Topic: Basic api usage

Posted under e621 Tools and Applications

I'm trying to make api requests to posts.json. I'm sure there is smth wrong with me building my request, I've read the api usage, but getting just /posts.json does not work for me. Can somebody just present the perfect example of query params and headers (if needed) for me to just pull posts?

bitWolfy

Former Staff

syranheli said:
I'm trying to make api requests to posts.json. I'm sure there is smth wrong with me building my request, I've read the api usage, but getting just /posts.json does not work for me. Can somebody just present the perfect example of query params and headers (if needed) for me to just pull posts?

What language are you using? I have an annotated example for node.js here: https://gist.github.com/bitWolfy/0707db0dca84bf3590b57aa8aad2ada2
Generally speaking, the API follows the same patterns as the site itself. So, for example, if you are looking for posts tagged with horse + solo + male, you open https://e621.net/posts.json?tags=horse+solo+male. You can also set the number of posts per page by adding the limit parameter, and the current page by setting the page parameter.

So, the second page of the horse + solo + male search, with 100 posts per page, looks like this: https://e621.net/posts.json?tags=horse+solo+male&limit=100&page=2.

You do need to set the User-Agent header, though. How exactly it's done depends on the language you are using.

bitwolfy said:
What language are you using? I have an annotated example for node.js here: https://gist.github.com/bitWolfy/0707db0dca84bf3590b57aa8aad2ada2
Generally speaking, the API follows the same patterns as the site itself. So, for example, if you are looking for posts tagged with horse + solo + male, you open https://e621.net/posts.json?tags=horse+solo+male. You can also set the number of posts per page by adding the limit parameter, and the current page by setting the page parameter.

So, the second page of the horse + solo + male search, with 100 posts per page, looks like this: https://e621.net/posts.json?tags=horse+solo+male&limit=100&page=2.

You do need to set the User-Agent header, though. How exactly it's done depends on the language you are using.

I'm trying to make requests from the front side (Angular) using http client or usual fetch. Which user agent header should I need to set?

"Set the header to your own value. Include your name and the script version in it" I'm not pretty sure which format should I use and what the "script version" means

bitwolfy said:
What language are you using? I have an annotated example for node.js here: https://gist.github.com/bitWolfy/0707db0dca84bf3590b57aa8aad2ada2
Generally speaking, the API follows the same patterns as the site itself. So, for example, if you are looking for posts tagged with horse + solo + male, you open https://e621.net/posts.json?tags=horse+solo+male. You can also set the number of posts per page by adding the limit parameter, and the current page by setting the page parameter.

So, the second page of the horse + solo + male search, with 100 posts per page, looks like this: https://e621.net/posts.json?tags=horse+solo+male&limit=100&page=2.

You do need to set the User-Agent header, though. How exactly it's done depends on the language you are using.

Just tried your fetchPosts function with User-Agent "MyProject/1.0 (by Syranheli on e621)" and got 403 (Forbidden). I need the propper User Agent header value format

bitWolfy

Former Staff

syranheli said:
I'm trying to make requests from the front side (Angular) using http client or usual fetch. Which user agent header should I need to set?

Ah. Browsers don't allow you to override the user-agent header. In this case, you should set an additional _client query parameter.
Like this: https://e621.net/posts.json?tags=horse+solo+male&_client=<your.UserAgent>

syranheli said:
"Set the header to your own value. Include your name and the script version in it" I'm not pretty sure which format should I use and what the "script version" means

It can be just about anything you want. For example, my userscript has the following User Agent: bitwolfy/re621:userscript/1.4.

bitwolfy said:
Ah. Browsers don't allow you to override the user-agent header. In this case, you should set an additional _client query parameter.
Like this: https://e621.net/posts.json?tags=horse+solo+male&_client=<your.UserAgent>

It can be just about anything you want. For example, my userscript has the following User Agent: bitwolfy/re621:userscript/1.4.

I'm trying to do it using your fetchPosts function with "syranheli/rpg621:userscript/1.0" user agent header and still get 403 forbidden

Maybe, I should save that header value somewhere on the site in my account api settings or is it forbidden to make requests from localhost?

bitWolfy

Former Staff

syranheli said:
I'm trying to do it using your fetchPosts function with "syranheli/rpg621:userscript/1.0" user agent header and still get 403 forbidden

That example is for node.js, I'm not sure how well (if at all) it would work with Angular. And it definitely works with Node: https://i.imgur.com/Sp1RlVh.png

Here's a very basic working example in normal JavaScript:

fetch(`https://e621.net/posts.json?tags=horse+solo+male&_client=syranheli/rpg621:userscript/1.0`)
    .then((response) => response.json())
    .then((data) => { console.log(data); });

bitwolfy said:
That example is for node.js, I'm not sure how well (if at all) it would work with Angular. And it definitely works with Node: https://i.imgur.com/Sp1RlVh.png

Here's a very basic working example in normal JavaScript:

fetch(`https://e621.net/posts.json?tags=horse+solo+male&_client=syranheli/rpg621:userscript/1.0`)
    .then((response) => response.json())
    .then((data) => { console.log(data); });

I literally just pasted that fetch code to console and got CORS blocked

And yes, I tried to use your fetchPosts from node.js. I initiated npm packed in empty folder, installed node-fetch, created index.js and placed your code (https://gist.github.com/bitWolfy/0707db0dca84bf3590b57aa8aad2ada2) there, replaced the 'sample.fetch.script' with 'syranheli/rpg621:userscript/1.0', then run "node index" and got 403 forbidden

fetch(`https://e621.net/posts.json?tags=horse+solo+male&_client=syranheli/rpg621:userscript/1.0`)
    .then((response) => response.json())
    .then((data) => { console.log(data); });

Seems to be working only from e621 domain

bitWolfy

Former Staff

syranheli said:
Getting this one when using fetch from angular, and this https://prnt.sc/10niwx6 from fetch from new chrome page console

Oddly enough, I get the same thing, but only when running it from a new chrome page in incognito mode.
No idea what's up with that. Does the same thing work from like google.com?

I don't think that it's just blocking requests from localhost, since I run a dev version of another project of mine off that no problem.

bitwolfy said:
Oddly enough, I get the same thing, but only when running it from a new chrome page in incognito mode.
No idea what's up with that. Does the same thing work from like google.com?

I don't think that it's just blocking requests from localhost, since I run a dev version of another project of mine off that no problem.

Same on the google.com. As well as from the Mozilla

bitWolfy

Former Staff

syranheli said:
Same on the google.com. As well as from the Mozilla

I don't know what's going on then. Sorry.
You could try to bring it up with Kira, e621's developer, on Discord.

syranheli said:
What about 403 forbidden from node side? Any ideas?

Sorry, I see I am over a year late to this, but I myself was having issues with CORS errors last night, when I was trying to use the e6 API for the first time. (I'm using Jquery/NodeJS, I installed xhr2 via NodeJS and made XHR requests with the proper headers, etc)

After probably 8 hours of diagnosis, I realized that if you get a CORS error, most likely you're trying to make a request as a client, and you really shouldn't can't do that. Number two, the way I solved it (on top of moving to NodeJS/API server), is I sent Access-Control headers with the proper parameters.

These headers have to be sent AFTER the xhr.open() function but before the xhr.send() function to properly send the header response to CORS. The exact headers and params needed to be sent through these headers can be found on the internet, basically if you copy/paste the error that Chrome gives you in the debug console, the first result on Stackoverflow has the necessary information. Hope this helps.

  • 1