I have a simple and basic question.
I have this sample code:
request.AddHeader("authorization", "Bearer _XU6l1eaDs9NQRTcb5QG4m0-ab1F3Y29ikw");
request.AddParameter("application/json", "{\"panelists\":
[{\"name\":\"Mary\",\"email\":\"[email protected]\"},
{\"name\":\"Mike\",\"email\":\"[email protected]\"}]}", ParameterType.RequestBody);
I want to replace the hard coded values:
_XU6l1eaDs9NQRTcb5QG4m0-ab1F3Y29ikw
and
"{\"panelists\":
[{\"name\":\"Mary\",\"email\":\"[email protected]\"},
{\"name\":\"Mike\",\"email\":\"[email protected]\"}]}"
with 2 string variables:
IList<Panelist> panelists = parameters.panelists;
string bearer = parameters.bearer;
I am having a brain fart and cannot get it right, what is the correct way to concatenate these variables to the string?
I am doing:
request.AddHeader("authorization", "Bearer " + bearer);
request.AddParameter("application/json", panelists, ParameterType.RequestBody);
But that is not working right.
Thank you for your help.
UPDATE: OP has asked the question here C# format an array for post to Api
I need to format the list of values in a way that I can pass it as the request body.
You'd need to convert the IList back to a String? Hence wouldn't it be better to represent the JSON structure with a Model:
public class Panelist {
public string name { get; set; }
public string email { get; set; }
}
public class Root {
public List<Panelist> panelists { get; set; }
}
Add the JSON to the Model or populate the Model on initialization:
myJson = {\"panelists\":
[{\"name\":\"Mary\",\"email\":\"[email protected]\"},
{\"name\":\"Mike\",\"email\":\"[email protected]\"}]}";
Root myDeserializedClass = JsonConvert.DeserializeObject(myJson)
And then serialize the Model to a string:
request.AddParameter("application/json", JsonConvert.SerializeObject(myDeserializedClass), ParameterType.RequestBody);