Fetch - get HTML text as a response

686 Views Asked by At

I have a problem. I have rate-limit that returns HTML:

const Limiter = rateLimit({
  windowMs: 5 * 60 * 1000,
  message: "Too many requests created from this IP, please try again later.",
  max: 10
})

And I have fetch request in Client side that gets that Message:

      const body = { email, password }

      const res = await fetch("http://localhost:5000/api/auth/login", { 
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify(body)
      })

      const parseRes = await res.json()

      if(parseRes.JwtToken) {
        localStorage.setItem("JwtToken", parseRes.JwtToken)

        setAuth(true)
        toast.success("Login successfuly")
      } else {
        setAuth(false)
        toast.error(parseRes)
      } 

My problem here is that when I get json object back it displays on the screen. But when I get that message object it does nothing. It just shows that failure 429 in the console. My question here is how can I get/display that HTML that returns normally like I would get an JSON object.

1

There are 1 best solutions below

0
epascarello On

So before you parse the JSON check the status

if (!res.ok) {
  /* handle the error */
  return;
}
const parseRes = await res.json()