+-

我正在尝试调用一个由实习生调用数据的外部api的api.我写的代码是:
[HttpPost]
public IHttpActionResult Post()
{
string _endpoint = "https://someurl.com/api/v1/models?auth_token=mytoken";
var httpContext = (System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"];
string upload_id = httpContext.Request.Form["upload_id"];
string filename = httpContext.Request.Form["filename"];
string filesize = "1000";
//return this.Ok<string>(upload_id + " " + filename);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("upload_id", upload_id),
new KeyValuePair<string, string>("filename", filename),
new KeyValuePair<string, string>("filesize", filesize)
});
using (var httpClient = new HttpClient())
{
var response = httpClient.PostAsync(_endpoint, content).Result;
return Json(JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result));
}
}
然后,我通过ajax打电话给客户端以获取数据:
$.ajax({
url: '/api/tws',
type: 'POST',
data: { 'file': "EX-IGES.IGS", 'upload_id': "eb550576d2" },
success: function (response) {
console.log('response',response);
}
});
但是,它总是返回null.我已经验证了API调用的工作原理,并且一切正确.我对C#有点陌生.
最佳答案
查看您在参数“ File”中传递的ajax调用,但在C#中,您正在寻找“ Filename”
固定的ajax代码:
$.ajax({ url: '/api/tws',
type: 'POST',
data: { 'filename': "EX-IGES.IGS", 'upload_id': "eb550576d2" },
success: function (response) { console.log('response',response); }
});
点击查看更多相关文章
转载注明原文:调用httpClient.PostAsync返回null - 乐贴网