bgenc.net/content/posts/2022.12.17.firefox-xml-parsing-error-no-root-element.md
Kaan Barmore-Genç 094d9faad8
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add firefox xml parsing error
2022-12-17 21:38:45 -05:00

1.5 KiB

title date toc images tags
Solving "XML Parsing Error: no root element found" in Firefox 2022-12-17T21:27:37-05:00 false
dev
web

I've been seeing this error a lot while working on my project Bulgur Cloud. It seems to show up only on Firefox:

Error message in Firefox console. "XML Parsing Error: no root element found. Location: http://...701.jpg Line Number 1, Column 1

What is curious was that I was not actually loading the file mentioned in the error message. I tried looking up what the error might mean, but all that came up was very specific issues regarding some web frameworks, unrelated to what I'm using.

I later realized however, while I wasn't loading the file in the error message, I was actually sending a DELETE request. The Bulgur Cloud server then responds to this request with an empty 200 OK response. Turns out, if you send an empty response to Firefox, it tries to parse the body of the response even though it's empty and gives you this error.

To quiet this error, I started just sending an empty JSON response even for requests that don't actually need it. Here's some rust code to illustrate the solution:

#[derive(Debug, Serialize)]
pub struct EmptySuccess {
    status: &'static str,
}

// Using actix-web
fn empty_ok_response() -> HttpResponse {
    HttpResponse::Ok().json(EmptySuccess { status: "ok"})
}

This adds a {"status": "ok"} json to the response, which seems to be enough to make Firefox happy.