我是新手,从不使用REST API,直到我停留在帖子上,我都不知道如何在PostAsync中获取代码200。我做错了什么或想念什么?...
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);
}
这是我需要填写的参数。我正在尝试在邮递员中进行测试,但我从未使用过该应用程序。如何在Postman中添加这些参数的正确方法?
{
"SolicitantData": null,
"DemographicalData": {
"Name": "Nombre",
"MiddleInitial": "I",
"LastName": "Apellido Paterno",
"LastName2": "Apellido Materno",
"NickName": "",
"SSN": "123456789",
"BirthDate": "19xx-xx-xx",
"EmailAddress": "email@somedomain.com",
"DriverLicense": "",
"DeathDate": "",
"Addresses": [],
"PhoneNumbers": []
}
答案 0 :(得分:2)
您必须在令牌之前包括身份验证类型,因此您需要
替换代码的这一行以包括auth类型,例如Bearer
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", token);
使用
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", string.Format("Bearer {0}", token));