Search code examples
node.jsxmlhttprequestreportportal

Report Portal Does not Attach Logs to Report


I am working on a way to send report logs to report portal with screenshots attached. I am using XmlHttpRequest module to hit the report portal log endpoint. here is my code. But this code doesn't work, instead I get a "json_request_part is missing" response message. What am I doing wrong, I followed the docs here docs

  try {
    const request = new XMLHttpRequest();

    const body = {
    itemUuid: 'gsgasgh-dsjdbbdjsbd',
    launchUuid: 'hshu1qqqqjjhdh-ddjdh',
    time: '1555677888',
    message: 'failed',
    level: 40000,
    file: {
      name: 'screenshot.png',
    },

  };

    request.open('POST', `http://rp.com/my_project/log`, false); // `false` makes the request synchronous
    request.setRequestHeader('Content-Type', 'multipart/form-data');
    request.setRequestHeader('Authorization', `Bearer ${config.token}`);
    request.send(JSON.stringify(body));

    if (request.status >= 200 && request.status <= 299) {
      const { responseText } = request;
      console.log(responseText);
    } else {
      console.log(`Unexpected Response Returned: ${request.responseText}`);
    }
  } catch (error) {
    console.log(error.message);
  }
};

Solution

  • Sending binary data is a bit more complicated, you have to form your multipart request in special format. The format is pretty well described in this guide. Notice that first part of your request should contain JSON with log message and files names from another parts of multipart. In addition, there is already implementation for Javascript maintained by ReportPortal's team. You may also find this snipped helpful:

        buildMultiPartStream(jsonPart, filePart, boundary) {
        const eol = "\r\n";
        const bx = `--${boundary}`;
        const buffers = [
            // eslint-disable-next-line function-paren-newline
            Buffer.from(
                // eslint-disable-next-line prefer-template
                bx + eol + "Content-Disposition: form-data; name=\"json_request_part\""
                + eol + "Content-Type: application/json" + eol
                + eol + eol + JSON.stringify(jsonPart) + eol,
            ),
            // eslint-disable-next-line function-paren-newline
            Buffer.from(
                // eslint-disable-next-line prefer-template
                bx + eol + "Content-Disposition: form-data; name=\"file\"; filename=\"" + filePart.name + "\"" + eol
                + "Content-Type: " + filePart.type + eol + eol,
            ),
            Buffer.from(filePart.content, 'base64'),
            Buffer.from(`${eol + bx}--${eol}`),
        ];
        return Buffer.concat(buffers);
    }