2

Request mydomain/request returns json string like "accepted" or "declined"

I would use request in angular to get this string :

  getStatus(jobOfferId): Promise<string> {
    const url = "someurl";

    return this.httpClientService.get(url)
      .toPromise()
      .then(response => response as string);
  }

This would work in angular 2, but I've just upgraded to angular 4 and this request returns an error instead

"Http failure during parsing for https://mydomain/request"

10
  • can you include a sample of the response? Commented Sep 3, 2017 at 15:47
  • @ochi I did, it's just a single string wrapped in brackets " Commented Sep 3, 2017 at 15:50
  • 1
    @LazarLjubenović Nowhere in my code I mentioned using HttpClient class specifically. httpClientService is an abstract injection that handles http related stuff. The class itsself was migrated. Commented Sep 3, 2017 at 16:05
  • 1
    @LazarLjubenović currently httpClientService.get is the same as HttpClient.get but before migration it had different implementation Commented Sep 3, 2017 at 16:08
  • 1
    @Benedictus are you looking for something like this Commented Sep 3, 2017 at 16:44

1 Answer 1

3

JSON is the default expected response type. You can try forcing using "text" instead and print the result just to understand what is happening:

httpClient.get('http://some.com/endpoint/', { responseType: 'text' }).subscribe(result => {
    console.log(result);
});
Sign up to request clarification or add additional context in comments.

3 Comments

error TS2345: Argument of type '{ responseType: "text"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'responseType' are incompatible. Type '"text"' is not assignable to type '"json"'. Also, headers is not defined.
@PhilippLudwig I removed the optional header from the example however the responseType: ''text' is correct. You can check a similar example here. Also this answer was for Angular 4, Angular 5 is having a new http service
Confirming that this is the correct approach if your response type is plain text instead of JSON. You mention in a comment above that your httpClientService is an abstraction layer over angular's httpClient. If this is not working, the issue is likely in how you implement your abstraction.

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.