HttpClient with Headers

I have a Question for angular 5. I become an API Call to make this over angular but I become always a bad request from the API IIS Server.
This is the String
`With the Methode POST

On the http-Header: Accept: text/javascript

Content-Type: application/json

I’m Body: {encodedetUrl}

and this is my Code i take

getPosition(sql: string): Observable<Position[]> {

    let headers = new HttpHeaders().set('Content-Type', 'application/json');
headers = headers.set('Accept', 'text/javascript');

    let sqlstring = encodeURIComponent(sql);

    let string = '{"Befehl": "' + sqlstring + '","Datenbank": "DBScharnsteinTest", "Login": "username", "Passwort": "password", "PKMitarbeiterID": "2640"}';
    console.log();
    return this.httpClient.post<Position[]> (this.url, string, {headers});
    
  }

Can anybody Help me why that not work.

Thx

I would like to make three recommendations.

First you should not use string as a variable name because it is a type in typescript and therefore is a restricted word. Usually the compiler should tell you this is an error.

let body = `{"Befehl": "${sqlstring}","Datenbank": "DBScharnsteinTest", "Login": "username", "Passwort": "password", "PKMitarbeiterID": "2640"}`;

Second I would combine the Headers in one assignment:

let headers = new HttpHeaders({
  'Content-Type': 'application/json',
  'Accept': 'text/javascript'
});

Third you use console.log() without something to log out. You can use a combination of text and variables like this way: console.log('POST: ', body);

Viel Erfolg :slight_smile:

1 Like

hello
Thx for the Answer.
sprichst du deutsch?

Yes I am speaking german. But we should not communicate in german in the thread. If your english is not that good and you prefer to speak in german, you can send me a private message. But for the community it would be better to stay with english and solve the problem in this thread :wink: .

2 Likes

Hello
I have solved the Problem. It was a missmatch in the Middleware Service. He has not Accept the Domain.
We have edit and it works! Thx