Home > database >  how to pass api key along with the post request
how to pass api key along with the post request

Time:11-07

Im using angular and have post request, I got unauthorize error which turned out i need to pass the api key as well,here is my post request:

    SendToBackEnd(){         
    var x=new mymodel();
    x.settlementCurrency='EUR';
    x.customerReference='m455';
    x.paymentReference='65465465';
    this.http.post("address",x).subscribe(s=>{
    });
  }

Could you please help me with that?no idea how i should pass the api key along with my request

CodePudding user response:

Where is located address provide parameter for Auth to your end point

var address = 'https://adressToEndpoint.com/?apiKey=key 

  SendToBackEnd(){         
        var x=new mymodel();
        x.settlementCurrency='EUR';
        x.customerReference='m455';
        x.paymentReference='65465465';
        this.http.post("address",x).subscribe(s=>{
        });
      }

CodePudding user response:

This worked for me. I create a function for headers, so I can reuse it anywhere. You might want to put this function in a helper file to reuse it.

  createAuthorizationHeader(headers: Headers) {
    headers.append('Content-Type', 'application/json');
    headers.append('api-key', `xxxxxxxxxxxxxxxxxxxx`);
  }


  SendToBackEnd(){
    const header = new Headers();
    this.createAuthorizationHeader(header);
     
    var x=new mymodel();
    x.settlementCurrency='EUR';
    x.customerReference='m455';
    x.paymentReference='65465465';
    this.http.post("address",x,
    {
      headers: header
     }).subscribe(s=>{
    });
  }

Please note that api-key should named the way your server expects it to be e.g apiKey.

  • Related