Search code examples
c#jsonangularangular2-services

Angular2 HTTP Post - Hitting the Service but no payload


I am trying to post to my back end service to delete some records. Right now I can hit the controller method on the backed but the parameters are null. (Our backed is a .NET CORE API). I notice in the dev tools that when I make the call I am getting a 204 No Content message.

I am not sure if this is a problem with my back end or front end.

Component Method that initiates the post:

    delete(): void {
        this._contactService.deleteEmail(this.multipleRecords) 
            .subscribe(s => { this.isSuccessful = s; },
            error => this.errorMessage = <any>error);   

API Call in Angular Service:

    deleteEmail(emails: IMutlipleDelete[]): Observable<boolean> {
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        return this._http.post( this.url, JSON.stringify({emailList:emails}), options)
            .map((response: Response) => <boolean>response.json())
            .catch(this.handleError);
}

^ Here, I have tried different content types, I read online that the COORS policy doesn't accept "application/json" as a content type, but even if I change the type, the same result occurs.

And lastly my back end Method: (This is were my parameter is null)

    [HttpPost]
    public bool EmailDelete(string emailList)
    {
        return _contactEmails.DeleteEmailRecords(emailList);
    }

In my "DeleteEmailRecords" method would deserialize the json object into the model that I need to delete. However, it never gets there because my initial parameter emailList is null.

I've tried several different things with my back end method:

    [HttpPost]
    public bool EmailDelete(string[] emailList)
    {
        return _contactEmails.DeleteEmailRecords(emailList);
    }

    [HttpPost]
    public bool EmailDelete(EmailDeleteModel[] emailList)
    {
        return _contactEmails.DeleteEmailRecords(emailList);
    }

Where EmailDeleteModel is the same model as IMutlipleDelete ~ just called a different name on the back end.

Most recently I tried this:

    [HttpPost]
    public bool EmailDelete([FromBody]EmailDeleteModel[] emailList)
    {
        return _contactEmails.DeleteEmailRecords(emailList);
    }

Still my parameter is not coming across, but am hitting the breakpoint in the method.

I've also tried different ways to set up my body, such as:

JSON.stringinfy(emailList:emailList); ~ and a ton of variations of this.

I also resorted to just try and passing a string as the emailList:

    let emails: string = "Dog";
    JSON.stringify({ emails})

But the 204 No Content is still an issue.

EDIT: Added a Console.Log(JSON.stringify({ emailList: emails }) (This is my IMultipleDelete) for clarity:

    {"emailList":
       [{
         "type":"Personal",
         "envelopeSalutation":"NANCY WALKOWIAK",
         "entityId":40075275,
         "affiliation":1,
         "toDelete":true,
         "accountId":5004528
       }]
    }        

Is their just something I am missing? Or am I way off base?

Thanks for your help.


Solution

  • Change your API function signature to something like this:

    EmailDelete([FromBody]MutlipleDelete[] emailList)
    

    The [FromBody] is important here.

    And, You may want to change the post call back to the way you had it before, too.

    return this._http.post( this.url, JSON.stringify(emails), options)... 
    

    Don't wrap the value you're passing in an object ({...})