Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem sending files via HTTP request for TestRail. #141

Open
fabio-morais-Exaud opened this issue Jan 25, 2023 · 0 comments
Open

Problem sending files via HTTP request for TestRail. #141

fabio-morais-Exaud opened this issue Jan 25, 2023 · 0 comments

Comments

@fabio-morais-Exaud
Copy link

fabio-morais-Exaud commented Jan 25, 2023

Hello.

I'm working on automating e2e test results to Testrail and I'm currently having problems sending files as attachments.

Testrail API allows to send files via a multi-part/form-data, but I having trouble writing the request.

Here is the code:

 /**
   * Post request to upload a file using multipart/form-data
   * See https://www.npmjs.com/package/unirest#request-form-methods
   * See https://support.gurock.com/hc/en-us/articles/7077196481428-Attachments#addattachmenttorun
   * @param api - command to pass in the HTTP request
   * @param attachment - the attachment Interface that contains info of the screenshot
   * @param callback - function that executes after getting the result from the HTTPS request.
   * @param error - function to execute after error occours
  */
  private _sendFile(api: String, attachment: TestRailAttachment,  callback: Function, error?: Function) {
    console.log(attachment);
    //let form = new FormData();
    //form.append('attachment', fs.createReadStream(attachment.path));
    //console.log(form);
    request("POST", `${this.base}/api/v2/${api}`)
    .headers({
      'content-type': 'multipart/form-data'
    })
    .field({'parameter' : 'value'})
    .attach({
      'attachment': fs.createReadStream(attachment.path) 
    })
    .auth( this.options.username, this.options.password)
    .then((res) => {
      console.log(res.body)
      callback(res.body);
    });
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant