0

I'm trying to retrieve information from a web service through an angular app, if I try from the browser everything is ok see image 200 OK if I try from angular I get a 401 no authorized. Could anyone help me?

COMPONENT.TS

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { MissioniService } from '../services/servizi/missioni.service';

@Component({
  selector: 'app-servizi',
  templateUrl: './servizi.component.html',
  styleUrls: \['./servizi.component.css'\]
})
export class ServiziComponent implements OnInit {
  obs:Observable<any>;


  constructor(private service:MissioniService) { }

  ngOnInit(): void {

  const host='https://username:password@xmlhostname.com/11428442.xml';

  //this.obs = this.service.request(host,{par : 1},'GET');
  this.obs = this.service.request(host,{},'GET');
    this.obs.subscribe((data:any) => {
      if(!data.error){
        
      }else{
        alert(data.error);
      }
    });
  }
}

SERVICE.TS:

export class MissioniService {

  constructor(private http:HttpClient) { }

  request = (url:string, parameters:any, method:string): Observable <any>=> {
    return this.http.request(method, url, {
        headers: {
            'Content-Type': 'application/json'
        },
        params: parameters
    }).pipe(
        /*
          map((data)=>{}
        */
        catchError(error => {
            return error;
        })
    );
  };
}

via angular App via browser

Saimon
  • 25
  • 3
  • You need to add Authorization header. The one that looks like `Basic dmlv...` when done via browser – Andrew Allen Dec 12 '22 at 15:59
  • the browser ask me the credentials and it works fine ... I don't know how to send the credentials via angular httpClient – Saimon Dec 12 '22 at 17:18
  • Does this answer your question? [Angular 6 HTTP Get request with HTTP-Basic authentication](https://stackoverflow.com/questions/53613529/angular-6-http-get-request-with-http-basic-authentication) – Andrew Allen Dec 12 '22 at 17:36
  • well, seems it solved the authorization issue but now return CORS error type xhr :( – Saimon Dec 12 '22 at 18:08

0 Answers0