I am trying to get the joke from https://icanhazdadjoke.com/. This is the code I used
const getDadJoke = async () => {
const res = await axios.get('https://icanhazdadjoke.com/', {headers: {Accept: 'application/json'}})
console.log(res.data.joke)
}
getDadJoke()
I expected to get the joke but instead I got the full html page, as if I didn’t specify the headers at all. What am I doing wrong?
3
Answers
Using
Axios
REST API call which response JSON format.If you using API from
https://icanhazdadjoke.com/api#authentication
, you can use
Axios
.Here is example.
Alternative method.
You needs to use web scrapping method for this case. Because HTML response from
https://icanhazdadjoke.com/
.This is example how to scrap using
puppeteer
library innode.js
Demo code
Save as
get-joke.js
file.Selector
Main Idea to use DOM tree
selector
In the Chrome’s DevTool (by pressing F12), shows HTML DOM tree structures.
<p> tag has class name is
subtitle
Install dependency and run it
Result
You can get the joke from that web site.
The HTML page you’re getting is a 503 response from Cloudflare.
As per the API documentation
My guess is they have a Cloudflare Browser Integrity Check configured that’s triggering for the default Node / Axios user-agent.
Setting a custom user-agent appears to get around this…
Given how unreliable Axios releases have been since v1.0.0, I highly recommend you switch to something else. The Fetch API is available natively in Node since v18
If you look at the API documentation for icanhazdadjoke.com, there is a section titled "Custom user agent." In that section, they explain how they want any requests to have a User Agent header. If you use Axios in a browser context, the User Agent is set for you by your browser. But I’m going to go out on a limb and say that you are running this code via Node, in which case, you may manually need to set the User Agent header, like so:
The docs say what they want you to put for the User Agent, but I think it would honestly work if there were any User Agent field at all.