Jakub Michálek
Jakub Michálek

Reputation: 163

How to keep line breaks in POST message

When i get the content of an email it is formatted with line breaks but when i send it to the API the result is text with no white space.

Is there any way to keep the spacing?

Edit:

const postNewTask = (taskTitle, taskComment, workerId) => {
  const { projectId, tasklistId } = dataStore();

  const { userEmail, apiKey } = credentialsStore();

  const options = {
    method: 'POST',
    headers: {
      Authorization:
        'Basic ' + Utilities.base64Encode(`${userEmail}:${apiKey}`),
    },
  };

  if (taskComment && workerId) {
    options.payload = JSON.stringify({
      name: taskTitle,
      comment: {
        content: taskComment,
      },
      worker: parseInt(workerId),
    });
  }

Upvotes: 0

Views: 2252

Answers (1)

Jakub Michálek
Jakub Michálek

Reputation: 163

I solved it. I just had to use taskComment.replace(/(?:\r\n|\r|\n)/g, '<br>') and the API knew what to do from there.

Final working code:

const postNewTask = (taskTitle, taskComment, workerId) => {
  const { projectId, tasklistId } = dataStore();

  const { userEmail, apiKey } = credentialsStore();

  const options = {
    method: 'POST',
    headers: {
      Authorization:
        'Basic ' + Utilities.base64Encode(`${userEmail}:${apiKey}`),
    },
  };

  if (taskComment && workerId) {
    options.payload = JSON.stringify({
      name: taskTitle,
      comment: {
        content: taskComment.replace(/(?:\r\n|\r|\n)/g, '<br>'),
      },
      worker: parseInt(workerId),
    });

Upvotes: 3

Related Questions