Sandeep Thomas
Sandeep Thomas

Reputation: 4759

Error in HTTP Post from Angular into Web API .NET Core

I'm trying to POST a JSON data into a webapi from Angular 9.

Here is my POST code

  const headers=new HttpHeaders().set("Content-Type",'application/json');
  let url:string=this.isServer?"":"https://localhost:44331/api";
  console.info(JSON.stringify(data));

  return this.client.post<any>(url+'/Crrequests',JSON.stringify(data),
  {headers,responseType:"json",withCredentials:true});

When I execute it triggers an error saying

"One or more validation errors occurred"

in developer console.

But I checked with POSTMAN the same set of JSON. It works fine.

So not having an ideas whats wrong with it.

Upvotes: 0

Views: 758

Answers (2)

okihnjo
okihnjo

Reputation: 438

Maybe this approach might help you, this is the way I make my Http requestes to my Backend. You could also subscribe to it and log it in the console so you can see the answer of your backend. Hope it helps

        var headers = new HttpHeaders({"Content-Type": "application/json"});
        var url= "https://localhost:44331/api";
        return this.client.post<any>(url + "/Crrequests", 
                 {
                    //  Json object you want to post, like
                    // Email: data.email
                    // Password: data.password
                 },
                 {
                     headers
                 }); 

Upvotes: 1

Ankush Jain
Ankush Jain

Reputation: 7079

Just post the data, no need to stringify

return (this.client.post<any>(url + "/Crrequests", data,{ headers, responseType: "json", withCredentials: true }));

Replace this JSON.stringify(data) with data only.

Upvotes: 1

Related Questions