1

All the examples I found in web are like this:

createArticle(article: Article): Observable<Article> {
     return this.http.post<Article>(this.url, article);
}

so they assume that web API's response contains Article. How to write above so that an Article is posted to web API and the response is string?

0

1 Answer 1

5

I came across this solution which solved the problem perfectly for me.

changing the return type is not enough, you need to add headers to your requst as bellow:

 createArticle(article: Article): Observable<any> {
    
        var headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    
        return this.http.post<any>(this._url, article, { headers, responseType: 'text' as 'json' })
            .pipe(catchError(err => {
                console.log(err);
                return Observable.throw(err);
            }));
}
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.