HTTP POST x-protobuf data using axios

216 views Asked by At

Previously I used request lib to send my requests, but since it is deprecated I'm moving to axios.

It looked like this:

const request = {
    url: url,
    method: 'POST',
    encoding: null,
    headers: {
      'content-type': 'application/x-protobuf',
    },
    body: requestEncoder.encode(data).finish(),
  };

  const {body} = await http.request(request);
  const results: any[] = [];
  const reader = protobuf.Reader.create(body as unknown as Uint8Array);
  while (reader.pos < reader.len) {
    results.push(responseEncoder.decodeDelimited(reader));
  }
  return results;

now in using axios I am doing the following:

  const response = await axios.post(
    url,
    requestEncoder.encode(data).finish(),
    {headers: {'content-type': 'application/x-protobuf'}, responseType: 'arraybuffer'});

  const results: any[] = [];
  const reader = protobuf.Reader.create( new Uint8Array(response.data) as unknown as Uint8Array);
  while (reader.pos < reader.len) {
    results.push(responseEncoder.decodeDelimited(reader));
  }
  return results;

However I get error:

 RangeError: index out of range: 98 + 101 > 149

Could anyone help figure out what I am doing wrong as it worked previously?

0

There are 0 answers