Quick Tip: API data lengths

Here’s a quick tip for troubleshooting API calls:

If you are calling an API and notice that data you are expecting isn’t being provided, verify that you are allocating enough space for the return values and telling it how much space you’ve allocated.

Many APIs require you to allocate space to hold the returned info – but if you don’t tell the API how much space you’ve provided, it will only return the information that fits in the space it knows about.

Case in point … a co-worker and I were trying to use the QDBRTVFD api … our code was allocating 32k (yeah, a lot more than needed) … but the data length we provided was only 400 bytes (size of the header data structure).

The information we were interested in was actually in one of the sub-structures … but the offset value for that sub-structure was zero.

A bit of digging turned up that the API was returning 400 bytes of data for the call (in the QDBFYRET field)… but there were 1400 bytes of data available (QDBFYAVL field).

The fix was quite simple … we told the API that we were providing 32k of space (same amount we were allocating) and it worked fine.

2 Replies to “Quick Tip: API data lengths”

  1. So you’re publicly stating when you write bad code, now? Interesting.

    Tip: Always set the bytes provided = %size(return_Struct)

  2. So you’re publicly stating when you write bad code, now? Interesting.

    Well, strictly speaking, it wasn’t my code 🙂

    But, to paraphrase a movie quote …. “Why do we write bad code? So we can learn from our mistakes”.

    Tip: Always set the bytes provided = %size(return_Struct)

    That will only work if your return_struct is all the data that you want back. In this case we WERE passing the size of return_struct to the API, but the information we needed was in sub-structures that the return_struct had offsets to.

Leave a Reply

Your email address will not be published. Required fields are marked *