Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to get the correct format for the json array I need to pass to a post request.

The example code I got shows the following format when using c# as the preferred programming language:

var client = new RestClient("https://api.us/v2/webinars/94395753143/panelists");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/json");
request.AddHeader("authorization", "Bearer _XU6l1eaDs9NQRTcb5QG4m0-ab1F3Y29ikw");
request.AddParameter("application/json", "{"panelists":[{"name":"Mary","email":"maryjkdfdsgfshdgf@jdfdkjdglfk.jkfgdj"},{"name":"Mike","email":"dfdsgfsdhf@jkgfdgfkdhgfdjg.fkjgdf"}]}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

As proof of concept, I am getting the values from an angularjs view formatting the json array, and passing them through the post request:

$http(request)
    .then(function successCallback(data) {
    
        angular.forEach(data.data, function (item) {
            
            var b = {
                name: item.Name,
                email: item.Email
            };
            $scope.arr.push(b);         
        });

        var parData = JSON.stringify({ 'panelists': $scope.arr, 'id': $scope.webinarId, 'bearer':  $scope.bearer});

    $http.post('/api/AddPanelists', parData)
        .then(function (data) {
    }), function (data) {
        alert("An error occurred during the request");
    };

In my C# code I have a function that receives parDAta and retrieves each of the 3 elements passed (array, id, bearer)

    [HttpPost]
    public void CreatePanelists(Newtonsoft.Json.Linq.JObject data)
    {
        Parameters parameters = JsonConvert.DeserializeObject<Parameters>(data.ToString());

        IList<Panelist> panelists = parameters.panelists;  << this value does not seem right
        string webId = parameters.id;      << this value is fine
        string bearer = parameters.bearer; << this value is fine

        var client = new RestSharp.RestClient("https://api.us/v2/webinars/" + webId + "/panelists");
        var request = new RestRequest(Method.POST);
        request.AddHeader("content-type", "application/json;charset=UTF-8");
        request.AddHeader("authorization", "Bearer " + bearer);
        request.AddParameter("application/json", panelists, ParameterType.RequestBody);
        IRestResponse response = client.Execute(request);
        var content = response.Content;

    }
    
    public class Panelist
    {
        public string name { get; set; }
        public string email { get; set; }
    }
    public class Parameters
    {
        public IList<Panelist> panelists { get; set; }
        public string id { get; set; }

        public string bearer { get; set; }
    }

When I check the console.log in the browser, to see if JavaScript is formatting the json is correct, I see this:

{"panelists":[{"name":"Jack Anderson","email":"janderson@email.com"},{"name":"Ed Johnson","email":"ejohnson@email.com"},{"name":"Dead Poole","email":"dpoole@email.com"},{"name":"Hank  Schmidt","email":"hschmidt@email.com"},{"name":"Steven Alves","email":"salves@email.com"},{"name":"Nilvio Alexander","email":"nalexanderemail.com"}],"id":94395753143,"bearer":"U19hW2pkQkO2A0Zv5EXz-h4kXJ56s"}

When I check the value once it makes it to the C# side of things, the string looks a bit different:

When I check what has been passed, here:

public void CreatePanelists(Newtonsoft.Json.Linq.JObject data) << here

The value looks:

{{
  "panelists": [
    {
      "name": "Jack Anderson",
      "email": "janderson@email.com"
    },
    {
      "name": "Ed Johnson",
      "email": "ejohnson@email.com"
    },
    {
      "name": "Dead Poole",
      "email": "dpoole@email.com"
    },
    {
      "name": "Hank  Schmidt",
      "email": "hschmidt@email.com"
    },
    {
      "name": "Steven Alves",
      "email": "salves@email.com"
    },
    {
      "name": "Nilvio Alexander",
      "email": "nalexander@email.com"
    }
  ],
  "id": 94395753143,
  "bearer": "U19hW2pkQkO2A0Zv5EXz-h4kXJ56s"
}}

I see extra curly brackets at the beginning and at the end of the string.

My goal is to be able to pass an array that looks exactly as the 3rd party api needs the array to be formatted, I do not think I am doing it correctly.

This is the format I need to have, specifically for the panelist string

{
  "panelists": [
    {
      "name": "Jack Anderson",
      "email": "janderson@email.com"
    },
    {
      "name": "Ed Johnson",
      "email": "ejohnson@email.com"
    },
    {
      "name": "Dead Poole",
      "email": "dpoole@email.com"
    },
    {
      "name": "Hank  Schmidt",
      "email": "hschmidt@email.com"
    },
    {
      "name": "Steven Alves",
      "email": "salves@email.com"
    },
    {
      "name": "Nilvio Alexander",
      "email": "nalexander@email.com"
    }
  ]
 }

Thank you for reading my question, I hope I can get a bit of help. If I need to clarify anything, please just let me know.

Thanks, Erasmo

UPDATE

screen shot

I think the extra curly braces are gone, but how do I know that the string to be passed will look like this:

{
  "panelists": [
    {
      "name": "Jack Anderson",
      "email": "janderson@email.com"
    },
    {
      "name": "Ed Johnson",
      "email": "ejohnson@email.com"
    },
    {
      "name": "Dead Poole",
      "email": "dpoole@email.com"
    },
    {
      "name": "Hank  Schmidt",
      "email": "hschmidt@email.com"
    },
    {
      "name": "Steven Alves",
      "email": "salves@email.com"
    },
    {
      "name": "Alejandro Nava-Gomez",
      "email": "anava-gomez@cityofmadison.com"
    },
    {
      "name": "itortu",
      "email": "itortu@gmail.com"
    }
  ]
 }

If you see the screen shot, I am not sure it looks correct. May be I am looking at the output and not understanding what the actual value will be passed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can create new object to satisfy expected structure.

[HttpPost]
public void CreatePanelists([FromBody] Parameters parameters)
{
    var webId = parameters.id; 
    var bearer = parameters.bearer;
    var data = new
    {
        panelists = parameters.panelists
    };

    var client = 
        new RestSharp.RestClient($"https://api.us/v2/webinars/{webId}/panelists");
    var request = new RestRequest(Method.POST);
    request.AddHeader("content-type", "application/json;charset=UTF-8");
    request.AddHeader("authorization", $"Bearer {bearer}");

    request.AddParameter("application/json", data, ParameterType.RequestBody);

    IRestResponse response = client.Execute(request);
    var content = response.Content;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...