Earlier quoted context omitted.
Unfortunately I have vivid memories of one API I worked with (many years ago) that included entire encoded XML documents as attribute values within top level XML. At least there was only one level of recursion. Still, perhaps not as bad as using XML documents as database keys...
I saw that in HTML. Not from a professional developer but it still blow my mind. That was the final straw that made me quit the project.
API Practices If You Hate Your Customers
221–230 of 258 posts
Re: API Practices If You Hate Your Customers
#222Earlier quoted context omitted.
You don't have to. Plenty of times, a 404 is all I need to know. Why make the client check two different locations?
If the 404 is due to the client calling the wrong URL, rather than trying to access an entity/record that doesn’t exist, the client code will incorrectly assume the entity/record doesn’t exist when it actually does. Eg an API has a /discountvoucher/ID, so your client can enter a discount voucher code and get info on the voucher. If your client code calls /voucher/ID instead, under the 404 approach you would incorrect…
There's the root of your error: the entity is the URL; the URL is the entity. If the client requests a URL which does not exist … that URL does not exist.
If the client requested a URL which does not fit the expected schema … that URL does not exist.
Once you embrace RESTfulness & HATEOAS, life gets so much simpler. Also, every time you return errors in a 200, God kills a kitten. If for nothing else, think of the kittens!
Re: API Practices If You Hate Your Customers
#223I was expecting to see two of my pet hates - returning a null array to represent no items, and returning a single object without an array to represent one item. I also once worked with an API where you had to send the data in POST format - abc=123&def=456. After much pressure from their customers, they finally relented and added an XML version of their API... where your request could look like this: abc=123&def=456 .…
“returning a single object without an array to represent one item.” I saw the same thing when working with PHP consuming data from a SOAP endpoint. First I thought PHP is stupid. But then I realized that XML can’t model single item arrays versus single objects. They look the same. JSON is better that way. You can model empty arrays and single item arrays.
Re: API Practices If You Hate Your Customers
#224Earlier quoted context omitted.
>I had to work with an API where the company decided everything should return http code 200 (well, at least all 4XX errors), and give the error code in the JSON response, mixing existing 4XX errors and their own errors. So here's the deal with this pattern...If you're returning a typed error response, something the client application should interpret, you want to be able to know which error responses will actually ha…
The client should try to parse the response body only if it has the appropriate Content-Type header value. It should not assume that responses with various status codes have a particular body format.
Re: API Practices If You Hate Your Customers
#225Earlier quoted context omitted.
“returning a single object without an array to represent one item.” I saw the same thing when working with PHP consuming data from a SOAP endpoint. First I thought PHP is stupid. But then I realized that XML can’t model single item arrays versus single objects. They look the same. JSON is better that way. You can model empty arrays and single item arrays.
You can force PHP to handle sngle-element arrays consistently, with the SOAP_SINGLE_ELEMENT_ARRAYS option, the behaviour is much saner when enabled.
Re: API Practices If You Hate Your Customers
#226Earlier quoted context omitted.
What would you suggest instead of an empty array for a 0 item return?
The person you replied to doesn't like returning "null". He/she would probably prefer returning an array that contains 0 items.
Re: API Practices If You Hate Your Customers
#227I was totally expecting to see something about using a protocol in an unexpected way, because "the protocol is not good enough". I had to work with an API where the company decided everything should return http code 200 (well, at least all 4XX errors), and give the error code in the JSON response, mixing existing 4XX errors and their own errors. When pointed out, the support answer was "we chose to give meaningful er…
Re: API Practices If You Hate Your Customers
#228Earlier quoted context omitted.
This is not true. Counterpoint, responses like 404 or 503 Bad Gateway do not come from the destination server. They do not indicate the intended server received your request.
404 and 500 should come from the destination server. You are correct that 503 would not.
Re: API Practices If You Hate Your Customers
#229Re: API Practices If You Hate Your Customers
#230Earlier quoted context omitted.
No, the "completely invalid query" is 400. 404 is only for "the request makes sense but that specific resource doesn't exist".
In this case it’s a 404 because the query URL pattern doesn’t exist - the most common cause of 404s.