Reputation: 67
I'm new at this never use REST API before I just stuck on the post I don't know how to get the code 200 in the PostAsync. What I doing wrong or what I missing?....
public async Task<ActionResult> Create([Bind(Include = "IndividualId,SolicitantDataView,TerminalId,OGPCorrelationID,OGPATGNumber,Source,UserId,SendByEmail")] RequestViewModel requestViewModel)
{
var token = await GetToken();
RequestModel requestModel = new RequestModel(requestViewModel);
string Baseurl = "https://exampleapiservice.com/api";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(Baseurl);
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
var content2 = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("IndividualId", requestViewModel.IndividualId),
new KeyValuePair<string, string>("OGPATGNumber", requestViewModel.OGPATGNumber),
new KeyValuePair<string, string>("OGPCorrelationID", requestViewModel.OGPCorrelationID),
new KeyValuePair<string, string>("Source", requestViewModel.Source),
new KeyValuePair<string, string>("UserId", requestViewModel.UserId),
new KeyValuePair<string, string>("TerminalId", requestViewModel.TerminalId.ToString()),
new KeyValuePair<string, string>("SendByEmail", requestViewModel.SendByEmail.ToString()),
new KeyValuePair<string, string>("Name", requestViewModel.SolicitantDataView.Name),
new KeyValuePair<string, string>("MiddleInitial", requestViewModel.SolicitantDataView.MiddleInitial),
new KeyValuePair<string, string>("LastName", requestViewModel.SolicitantDataView.LastName),
new KeyValuePair<string, string>("LastName2", requestViewModel.SolicitantDataView.LastName2),
new KeyValuePair<string, string>("SSN", requestViewModel.SolicitantDataView.SSN),
new KeyValuePair<string, string>("BirthDate", requestViewModel.SolicitantDataView.BirthDate.ToString()),
new KeyValuePair<string, string>("EmailAddress", requestViewModel.SolicitantDataView.EmailAddress),
new KeyValuePair<string, string>("DriverLicense", requestViewModel.SolicitantDataView.DriverLicense)
});
var myContent = JsonConvert.SerializeObject(content2);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage Res = await client.PostAsync("/api/getcertification ", byteContent);
}
return View(requestModel);
}
This is the parameters I need to fill. I'm trying to test in Postman but I never use that app. How is the correct way to add those parameters in Postman??
{
"SolicitantData": null,
"DemographicalData": {
"Name": "Nombre",
"MiddleInitial": "I",
"LastName": "Apellido Paterno",
"LastName2": "Apellido Materno",
"NickName": "",
"SSN": "123456789",
"BirthDate": "19xx-xx-xx",
"EmailAddress": "[email protected]",
"DriverLicense": "",
"DeathDate": "",
"Addresses": [],
"PhoneNumbers": []
}
Upvotes: 2
Views: 3715
Reputation: 2348
you have to include authentication type before the token so you need to
replace this line of the code to include auth type e.g. Bearer
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
with
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", string.Format("Bearer {0}", token));
Upvotes: 2