0

I call http post method from angular4 component to web api. When I print those values return from web api on console it prints empty values. I checked the request using postman also. It also didn't work.

Http post call from component file.

EditValue(event, name, id) {
  const headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
  const options = new RequestOptions({ headers: headers });
  let body = { "name" : name, "id": id }
  this.http_.post('http://localhost:12412/api/employee?', body, options)
    .subscribe((data) => {console.log(data)});
}

Http post method in web api.

[HttpPost]            
public string postemp(Model model)
{
    return "name:" +model.name + " , " + "id: " + model.id;
}

public class Model
{
    public int id { get; set; }
    public string name { get; set; }
}

When I check the name and id from Editvalue function those values are passing. I tried solutions given by Angular4 http post method does not pass data to web api also. But issue didn't fix.How to check those posted data is passing to web api post method?

I attached the response result I got below.

enter image description here

6
  • can you change the content-type ? ..,did you try that ? Commented Mar 14, 2018 at 8:57
  • Are you using HttpClient? Commented Mar 14, 2018 at 9:19
  • No.I used Http. Commented Mar 14, 2018 at 9:27
  • Did you check the incoming parameter values on your EditValue function? Can you try to log them in your console? Commented Mar 14, 2018 at 11:15
  • @E.L.N.D.Madubhashini is your question different from the question in Angular4 http post method does not pass data to web api? Commented Mar 14, 2018 at 12:22

4 Answers 4

1

You are sending the info in the body in JSON format. But in your header you set application/x-www-form-urlencoded in which the values are encoded in key-value tuples separated by '&', with a '=' between the key and the value. So, to be able to send the data in the JSON format you need to change the Content-Type in your header to application/json.

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

2 Comments

After I changing the content-type to application/json it gives error like "Failed to load localhost:12412/api/employee?: Response for preflight has invalid HTTP status code 405."
You need to use JSON.stringify() on the body element to get a JSON representation to use in the http post method.
1

Just use the [FromBody] attribute to your API method parameter like below.

[HttpPost]            
public string postemp([FromBody]Model model)
{
    return "name:" +model.name + " , " + "id: " + model.id;
}

Comments

0

try JSON.stringify(body) before you send to the webapi once

1 Comment

Still get the same result.
0

Since the content type I used here is 'application/x-www-form-urlencoded', body should not be passed using JSON object. Body I passed here should be in query string like below.

let body="id="+id+"&name="+name;

It worked for me.

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.