I think there's a pj_strdup3 or something that does this, and takes the data from a pool (so you don't need to remember to free it). Use the pool of the message you're building, I think.