0

I'm trying to get string response from my back application. i searched for this issue and almost in all the solutions they add { responseType: "text"} or {responseType: 'text' as 'json'} to get string, i added it but i can't arrive, here my angular code:

msg:any;
  func(){
    this.bookService.getRessource(url,{ responseType: "text"})
      .subscribe(data => {
        console.log(data)
        this.msg=data;
      }, err => {
        console.log(err)
      })
  }

This is my service, i have to add jwt to header:

getRessource(url,res){
   let headers=new HttpHeaders({'Authorization':'Bearer '+this.authService.jwt});
    return this.http.get(url,{headers:headers});
  }

The funtion add a book to user's favorite list, it works and add it to database but the weird thing is the error message is shown in the console and i can find the string that i would like to get in that error. But i can't comprehend how the function works and then the error message is shown!!

This is the error message i receive:

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:8080/user/addBookToUser?username=admin&id=9", ok: false, …}
2
  • 1
    Can you please share the error you are specifically receiving and also what does the network tab for that request indicate about the response in terms of the content-type and what the payload looks like? Also {responseType: 'text'} would be added as an option to this.http.get() alongside headers. You aren't specifying {responseType: 'text'} in the actual HttpClient call currently. Commented Mar 28, 2019 at 15:34
  • can you please add the server code that treats the request? Commented Mar 28, 2019 at 15:39

1 Answer 1

4

In your current code, you are not placing responseType: text in the correct place. It needs to be an option of the actual HttpClient get() alongside headers:

msg:any;

func() {
  this.bookService.getRessource(url)
    .subscribe(data => {
      console.log(data)
      this.msg = data;
    }, err => {
      console.log(err)
    })
}

getRessource(url) {
  const headers = new HttpHeaders({ 'Authorization': 'Bearer ' + this.authService.jwt });
  const options = { responseType: 'text', headers };
  return this.http.get(url, options);
}

Hopefully that helps!

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.