user6321478
user6321478

Reputation:

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" } 

Upvotes: 0

Views: 21133

Answers (6)

user19240600
user19240600

Reputation: 1

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

Use form-data instead of raw-json

Key - eventId
Value - "5808786fa3e9ec79546b3c71"

This worked for me.

Upvotes: 0

Mikael
Mikael

Reputation: 11

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

Upvotes: 1

Keith Wilson
Keith Wilson

Reputation: 21

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

Upvotes: 2

JR Strayhorn
JR Strayhorn

Reputation: 86

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.

Upvotes: 4

jimpaine
jimpaine

Reputation: 887

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" } 

Upvotes: 1

Ross Bush
Ross Bush

Reputation: 15185

Your payload is not valid.

Change-->

 { "Id" = "5808786fa3e9ec79546b3c71" } 

To-->

{ "Id" : "5808786fa3e9ec79546b3c71" } 

Upvotes: 0

Related Questions