In an XML document, you need to escape any ampersands in your text as
I began a new coding project. Sure, there’s documentation for the API that solves my problem. I find out it uses XML. Extensible Markup Language, a classic API format. Cool. I craft a beautiful script that works at first. Hallelujah!
Later on, I realize it doesn’t work as well as I believed. It turns out, if I want a server to accept my XML document, escaping certain characters might be required. The documentation didn’t mention this. It was my first time using XML, how would I know?
I noticed a script only worked for a handful of requests. It failed for most, returning a 400 status code. Suspecting the issue was likely in my payload, I studied the data of the request bodies that failed compared to the others that succeeded. All of the payload bodies that failed contained text with an ampersand.
Suspecting it might be an XML + ampersand related issue, I google this Stack Overflow post which explains the ampersand escaping situation. There are a handful of characters that must be escaped. Otherwise, you might not be able to connect to the server.
These are the things they often don’t tell you. Those little details you must sometimes realize for yourself, unless someone bothers to mention it or write it down. Now you know something that cost me an hour or two of tinkering to realize!
Wanna read more on HTTP? Check out my guide on making HTTP requests with Python to read more about HTTP requests.