0

I have this error 401 returning from API server (Authentication Error)

Can someone tell me what I'm doing wrong when passing the header?

  HEADER_ZENVIA: {
    "X-API-TOKEN": "xxx",
    "Content-Type": "application/json",
  },

My Code:

sendMessage(
    numeroDestino: string,
    nomeEmpresa: string,
    numeroVaga: string,
    nomeCliente: string,
    nomeRecrutadora: string
  ) {
    const contentsTemplate = {
      type: "template",
      templateId: this.templateID,
      fields: {
        nome_empresa: nomeEmpresa,
        numero_vaga: numeroVaga,
        nome_cliente: nomeCliente,
        nome_recrutadora: nomeRecrutadora,
      },
    };
    return this.http.post(`${API_ZENVIA_URL}`, {
      body: {
        from: BOT_NUMBER,
        to: `${numeroDestino}`,
        contents: [{ ...contentsTemplate }],
      },
      headers: { ...HEADER_ZENVIA },
    });
  }

PostMan Works: works

3
  • Since it's a 401, it's most likely an issue with the API_TOKEN. Check your request headers, see if both your headers went through. Also check if your API token is still valid. Maybe it expired. Commented May 10, 2021 at 2:01
  • If you copy the network request as a Curl command and import it into postman does it work (or is that what your postman example is)? Commented May 10, 2021 at 2:03
  • I tryed with post man and works. The problem is not API_TOKEN Commented May 10, 2021 at 2:08

1 Answer 1

1

If you want to set headers for an http.post you should use the HttpHeaders class

import { HttpClient, HttpHeaders } from '@angular/common/http';

// ...

const headers = new HttpHeaders()
  .set('Content-Type', 'application/json')
  .set('X-API-TOKEN', API_TOKEN);
const body = {
    from: BOT_NUMBER,
    to: `${numeroDestino}`,
    contents: [{ ...contentsTemplate }],
  }

     return this.http.post(API_ZENVIA_URL, body, { headers: headers })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.