0

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" } 
1
  • In the scenario shown, I would use a delete verb and take the id from the route, or do you have a specific need to pass it in the body? Commented Oct 20, 2016 at 17:49

6 Answers 6

4

I know this is an older question but I wanted to help those who might have a similar problem as I was able to get this working.

In WebAPI Controller my method is setup as

[HttpPost]
public IHttpActionResult Create([FromBody] int eventId)
{
    ....
}

In order to get this to test properly in Postman you have to: In body, set to raw, make sure JSON (application/json) is set and then just add value like 2 that's it.. not like { "eventId":2 } which is proper JSON just the value and then it will work.

So in original poster's case, in Postman, if you set Body to raw, JSON (application/json) then "5808786fa3e9ec79546b3c71" as value it will work.

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

Comments

2

In Postman ensure the body is set to raw and select json and in the body just write "your string" in quotes. Do not use {} to surround it because that is to make a complex object

Comments

1

Try the following in the body, with the content-type as application/json

{ "5808786fa3e9ec79546b3c71" }

As when you specify it like so, it will attempt to de-serialize into a complex type with a property of Id

{ "Id" : "5808786fa3e9ec79546b3c71" } 

1 Comment

I was also struggling with same issue. Just put "5808786fa3e9ec79546b3c71" into body and it worked for me.
1

Old question, but for those still wondering, I would recommend sending your string as a query parameter. Take a method like this for example:

    [AllowAnonymous]
    [HttpGet("resendEmailConfirmtionLink")]
    public async Task<IActionResult> ResendEmailConfirmationLink(string email)
    {
        var user = await _userManager.FindByEmailAsync(email);

        if (user == null) return Unauthorized();

        var origin = Request.Headers["origin"];
        var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
        token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));

        var verifyUrl = $"{origin}/verifyEmail?token={token}&email={user.Email}";
        var message = $"<p>Please click the below link to verify your email address:</p><p><a href='{verifyUrl}'>Click to verify email</a></p>";

        await _emailSender.SendEmailAsync(user.Email, "Please verify email", message);

        return Ok("Email verification link resent");
    }

This method expects a key value pair of a string called email. You can send your request like "http://localhost:5000/api/account/[email protected]" or, in Postman, add it as a parameter like this: postman query params

Comments

0

Your payload is not valid.

Change-->

 { "Id" = "5808786fa3e9ec79546b3c71" } 

To-->

{ "Id" : "5808786fa3e9ec79546b3c71" } 

1 Comment

I accidentally posted that as i read blogs about adding = without valuename "Id" etc.. but tried it and still NULL coming into web api
0
[HttpPost]
public IHttpActionResult Create(int eventId)
{
    ....
}

Use form-data instead of raw-json

Key - eventId
Value - "5808786fa3e9ec79546b3c71"

This worked for me.

1 Comment

Your answer could be improved by putting all code within a Code Sample by using the option in the edit window or by manually formatting the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.