Jeremy Miller Jeremy Miller - 1 month ago 17
C# Question

Postman Testing send string to Web Api accepting string is null

I have been testing all the Get,Create,Update methods with Postman in which the Get passes in nothing. The Create and Update passes in raw json with Activity object with several properties that do match up with the C# class

So this signature for Create and Update works fine

[HttpPost]
public IHttpActionResult UpdateActivity(Activity activity)


Above works with Postman passing in JSON content type with all the properties. I have done this on OTHER projects.

HOWEVER

I'm trying to simply pass in a string and it is null no matter what

public IHttpActionResult DeleteActivity([FromBody]string Id)
{
// delete
var del = ActivityService.DeleteActivity(Id);
return Ok(del);
}


Postman I tried MANY ways

http://localhost:49810/api/activityapi/deleteactivity


I have tried MANY many ways based on blogs and google search one such example

{ "Id" = "5808786fa3e9ec79546b3c71" }

Answer

For Postman --> Web Api ... You will want to drop [FromBody] when passing in string as json format. I have had this problem in the past.

So others are correct that you DO want this line in Postman

{ "Id" : "5808786fa3e9ec79546b3c71" } 

However, you WILL be getting null still as you need to change from

public IHttpActionResult DeleteActivity([FromBody]string Id)

TO

public IHttpActionResult DeleteActivity(string Id)