Home > other >  how to pass hardcoded guid in postman body section
how to pass hardcoded guid in postman body section

Time:01-04

I'm trying to pass 2 guid values to a .net api like below image enter image description here

if i pass like above image i'm not getting the values in .net like below image .. pls let me know the syntax to pass the hardcoded guid in postman

enter image description here

CodePudding user response:

You should the static method Guid.NewGuid() instead of calling the default constructor. This should work:

var ApplicationId = Guid.NewGuid();
var DistrictId = Guid.NewGuid();

CodePudding user response:

There is no GUID data type in JSON, so you can't use it directly.

Instead, you can use your parameter's datatype as 'string' in your model.

Then:

Solution 1

Optionally, you can define your parameters as string, then convert them into GUID in your method:

public class AuthenticateModel 
{
    //...
    public string ApplicationId { get; set; }

    public string DistrictId { get; set; }
    //...
} 

In your method:

public SecurityToken AuthenticateUser(AuthenticateModel authenticateModel)
{
    var applicationId = Guid.Parse(authenticateModel.ApplicationId);
    var districtId = Guid.Parse(authenticateModel.DistrictId);
}

Solution 2:

You can create 2 new variables:

public class AuthenticateModel 
{
    public string ApplicationId { get; set; }

    public string DistrictId { get; set; }

    [JsonIgnore] //this is for Newtonsoft.Json
    [IgnoreDataMember] //this is for default JavaScriptSerializer class
    public Guid ApplicationGuid { get => Guid.Parse(ApplicationId); set => ApplicationId = value.ToString(); }

    [JsonIgnore] //this is for Newtonsoft.Json
    [IgnoreDataMember] //this is for default JavaScriptSerializer class
    public Guid DistrictGuid { get => Guid.Parse(DistrictId); set => DistrictId = value.ToString(); }
} 

and then use it in your method:

public SecurityToken AuthenticateUser(AuthenticateModel authenticateModel)
{
    //...
    doSomething(authenticateModel.ApplicationGuid);
    doSomething(authenticateModel.DistrictGuid);
    //...
}

Hope it works for you.

  • Related