0

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.

2 Answers 2

1

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.

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

Comments

1

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
                 }); 

Comments

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.