Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Send email data to HTTP APIs #498

Open
2 tasks
Tracked by #9
ricardobrg opened this issue Jan 15, 2024 · 0 comments
Open
2 tasks
Tracked by #9

Send email data to HTTP APIs #498

ricardobrg opened this issue Jan 15, 2024 · 0 comments
Assignees

Comments

@ricardobrg
Copy link
Contributor

Reason

Email scrape feature needs to send the data to a server

Description

Implement a method to send email receipt data to the Postman Echo http api. It will follow the same pattern as #30 but instead of image upload it will send the following request:

Generic sample code. Need to be tested:

export default {
  methods: {
    async sendMultipartFormDataRequest(apiUrl, senderEmail, emailBody, attachments) {
      const formData = new FormData();

      // Append fields
      formData.append('senderEmail', senderEmail);
      formData.append('emailBody', emailBody);

      // Append attachments
      for (const attachment of attachments) {
        const file = await this.readFile(attachment);
        formData.append('attachments', file, attachment);
      }

      try {
        const response = await fetch(apiUrl, {
          method: 'POST',
          body: formData,
          headers: {
            'Content-Type': 'multipart/form-data',
          },
        });

        if (!response.ok) {
          throw new Error(`HTTP error! Status: ${response.status}`);
        }

        const responseData = await response.json();
        console.log('Response:', responseData);
      } catch (error) {
        console.error('Error:', error);
      }
    },

    async readFile(attachment) {
      return new Promise((resolve, reject) => {
        const reader = new FileReader();

        reader.onload = (event) => {
          resolve(new Blob([event.target.result]));
        };

        reader.onerror = (error) => {
          reject(error);
        };

        // Read the file as ArrayBuffer
        reader.readAsArrayBuffer(new Blob([attachment]));
      });
    },
  },
};

Success Criteria

  • the postman echo api responds with 200
  • the data is echoed by the api

Additional Information

No response

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants