Search code examples
angularhttpangular-httpclient

How to receive byte and string data at the same time?


Is it possible to receive byte and string data simultaneously in an HTTP response? A cannot mix the two put in a json but you get the idea:

private async getFile(): Promise<{ data: Blob, checksum: string }> {

  return this.http.get('some url/getfile...', {
    'headers': accessToken
  }).pipe(timeout(10000)).toPromise().then(res => {
    if (res['status'] === 200 && res['body']) {
      return {
        data: res['body']['data'],
        checksum: res['body']['checksum']
      };
    }
    return undefined;
  }).catch(() => undefined);

}

What's the proper way?


My ideas:

Should the server maybe put the checksum into the response header?

Or, should it send also the checksum as bytes? If so, how to separate it from the actual data?

Or what if I'd combine the two in a single byte stream and if e.g. md5 was used to produce the checksum I would know that the first (or last) 128 bits (= 16 bytes?) are the checksum, the rest is the actual data.

What I don't want is to (1) do it with separate requests (2) put the checksum in some text file on the server side and zip it with the actual file, then unzip the archive on the client side.


Solution

  • The Content-MD5 header is a thing and used for this purpose in both uploads and downloads: https://www.rfc-editor.org/rfc/rfc1864

    Generally, headers are for small bits of metadata relating to your request. This is a totally valid header use case if you feel this extra security is warranted.