0

Currently, I have to call everything like this, manually having to add the correct header to each request (and also have to manually stringify it seems..?). Is there a place I can define standard headers to be used for the whole application?

@Injectable()
export class MyService {

    constructor(private http:Http) {
    }

    doSomething(data:MyObj) {
        return this.http.post('/api/somebackend',
            JSON.stringify(data),
            {
                headers: new Headers({
                    'Content-Type': 'application/json'
                })
            })
            .map(e => e.json())
            // ....
    }
}
Matsemann
  • 21,083
  • 19
  • 56
  • 89

2 Answers2

1

Take a look at this question:

How to make Angular 2 send all requests as application/x-www-form-urlencoded

you can do the same just add the header 'Content-Type': 'application/json'.

Community
  • 1
  • 1
Langley
  • 5,326
  • 2
  • 26
  • 42
-1

Sorry for posting as an answer but I don't have enough rep to comment.

you could try using an interceptor like it's shown here. So you only define it in one place.

MarkovskI
  • 1,489
  • 2
  • 21
  • 25