I cannot get my header on POST request to work, I've tried pretty much every solution people posted on the internet.
here is the code:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders} from '@angular/common/http';
import { Observable } from "rxjs";
import { Headers } from '@angular/http';
@Injectable()
export class APIService {
constructor(private http:HttpClient) { }
function(url, var1, var2):Observable<any>{
let data = {
"status": var1,
"duration": var2
};
let message = JSON.stringify(data );
return this.http.post(url, message, <HELP>);
}
here are the things I've tried:
const headerDict = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type',
};
const requestOptions = {
headers: new Headers(headerDict),
};
return this.http.post(url, message, requestOptions );
and
return this.http.post(url, message, {headers:{'Content-Type': 'application/json'}});
and
//added above -> import { Headers, RequestOptions } from '@angular/http';
const headers = new Headers({ 'Content-Type': 'application/json' });
const options = new RequestOptions({ headers: headers });
return this.http.post(url, message, options );
and
var headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({ headers: headers });
return this.http.post(url, message, options );
and some others I don't remember. None of them worked. I've been stuck for hours. Most of the errors are:
"Argument of type 'RequestOptions' is not assignable to parameter of type '{ headers?: HttpHeaders; observe?: "body"; params?: HttpParams; (...) "
Types of property 'headers' are incompatible
Type 'Headers' is not assignable to type 'HttpHeaders'.
Property 'headers' is missing in type 'Headers'.
When I print "message" the output is what I want, but it won't get posted without the header (error 415 when I try posting on my API) and the only logical explanation is that the missing header is making it not work, but with a header it won't compile. Please help?
FYI: OS = Windows. I never had a problem before with my gets, only with this post attempt. I don't know if the OS may influence this so I thought it was better to let you know.
Headersfrom@angular/httpandHttpHeadersfrom@angular/common/httpare not the same thing. This should be clear: "Type 'Headers' is not assignable to type 'HttpHeaders'." In general, pick one client and stick to it. - jonrsharpeHttpClientModule, not theHttpModule, in your module imports. I'd strongly recommend reading the documentation: angular.io/guide/http - jonrsharpe