0

I am trying to send a Http.Post request from my Angular application to add an item ("user") to my database. Problem seem to be that my "add user" service storing a method providing this http.post request do not have any effect on my dotnet core API providing the backend. A similar structured Http.Get request is working fine and the dotnet core API backend is sending the data from the database. Also the dotnet core API backend seems working fine. Sending the same http.post request (same Body and same address) from Postman brings a new user to the database. Here is the code maybe you can help me:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { HttpHeaders } from '@angular/common/http';

@Injectable()
export class AddUserService {

  constructor(private _http: Http) { }
  addUsers() {
    console.log('MethodAddUseractive')
    return this._http.post('https://localhost:5001/api/Use',
          {"id":41,"name":"Dustin Nachname","email":"[email protected]", "genderid": 1,"bday":"1987-09-10T08:15:14.390102","registration_Day":"2019-12-08T00:00:00.000000"})
          .map(res => res.json());
  }


}

In addition the method adduser() is called as I can see from the Webconsole entry "MethodAddUseractive"

1 Answer 1

2

You didnt include any headers. Depending on your backend, you have to set the properties for the header. Something like var headers = new HttpHeaders({ "Content-Type": "application/json" }); return this._http.post('https://localhost:5001/api/Use', {"id":41,"name":"Dustin Nachname","email":"[email protected]", "genderid": 1,"bday":"1987-09-10T08:15:14.390102","registration_Day":"2019-12-08T00:00:00.000000"}, { headers } ) .subscribe(res=>console.log(res)); }

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it works. I switched to HttpClient and now I can add items to the database.

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.