C# LearnUPON-I';当调用我的插入API时,我得到404响应
我将把Learn OnAPI实现到我的.NET MVC应用程序中,他们给出了cURL,我必须将这些cURL URL转换为C#中的web请求,以便获取或发送数据到第三方Learn OnAPI服务器。列出API,删除API工作正常,但创建和更新API始终返回远程服务器返回错误:(404)未找到 由Learn on提供的卷曲样本: 根据上面的cURL,我在下面编写了API调用的web请求调用,这种模式在列出/删除API中起作用C# LearnUPON-I';当调用我的插入API时,我得到404响应,c#,asp.net-mvc,curl,C#,Asp.net Mvc,Curl,我将把Learn OnAPI实现到我的.NET MVC应用程序中,他们给出了cURL,我必须将这些cURL URL转换为C#中的web请求,以便获取或发送数据到第三方Learn OnAPI服务器。列出API,删除API工作正常,但创建和更新API始终返回远程服务器返回错误:(404)未找到 由Learn on提供的卷曲样本: 根据上面的cURL,我在下面编写了API调用的web请求调用,这种模式在列出/删除API中起作用 后模型: 创建API代码: API返回: { “消息”:“远程服务器返回错
注意:上述消息来自异常捕获块,每次异常发生时返回404响应。您是否在POST请求正文中发送任何数据 在我看来,作为一个几乎没有C#经验的人,似乎您正在将
mdlUser
数据作为HTTP请求头(称为data
)发送,而没有在POST正文中发送任何内容。在此问题中,有几个提出POST请求的示例:
这可能是404问题(或者至少是第一个问题!)。您捕获的异常是合法的异常(4xx通常被视为非正常状态)。如果要捕获404(例如,在检查门户中是否存在用户时,这可能是LearnUpon API的合法响应),请查看以下问题:
最后,请随时联系您的LearnUpon支持团队。我们可能没有开发人员需要的所有答案,但我们通常可以为您指出正确的方向
免责声明:我使用LearnUpon。“消息来自异常捕获块”您真的确定吗?该异常应返回HTTP 500状态。404似乎表明您的路由有问题,如果找不到适合您的帖子的路由,将出现404。@oerkelens,是的,我已将我的代码添加到try&catch块中。问题是,其他2个cURL使用相同的代码格式工作正常,但仅Insert&Update cURL返回“远程服务器返回错误:(404)未找到”,如果路由有任何问题,那么它也会在清单&Delete中返回。是否可能有一些代码真正解决这个问题?链接很有用,但可能无法访问。
curl -X POST -H "Content-Type: application/json" --user 988d4f1313f881e5ac6bfdfc7f54244aab : 905a12r3a0c -d '{"User": {"last_name" : "Upon",
"first_name" : "Learn", "email" : "learnuponapi@samplelearningco.com", "password" : "password1", "language" : "en", "membership_type" : "Member"
}}' https://yourdomain.learnupon.com/api/v1/users
{
"first_name": "Krishna",
"last_name": "Patel",
"email": "krishna.patel@c-metric.com",
"password": "1234567890",
"language": "en",
"membership_type": "Member",
"username":"krishna.patel"
}
[System.Web.Http.HttpPost]
public IHttpActionResult CreateUser(UserModel mdlUser)
{
string content = "";
try
{
if (ModelState.IsValid)
{
// target url given by Learn Upon, Unique for each account
string yourTarget = "https://{domain}/api/v1/users";
string josnUserModel = JsonConvert.SerializeObject(mdlUser);
josnUserModel = "{\"User\":" + josnUserModel + "}";
WebRequest yourWebRequest = WebRequest.Create(yourTarget);
yourWebRequest.Method = "POST";
// Establish credentials (if necessary)
CredentialCache yourCredentials = new CredentialCache();
yourCredentials.Add(new Uri(yourTarget), "Basic", new NetworkCredential(username, password));
// Set your credentials for your request
yourWebRequest.Credentials = yourCredentials;
// Add basic authentication headers (if necessary)
yourWebRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(String.Format("{0}:{1}", username, password))));
yourWebRequest.Headers.Add("data", josnUserModel);
WebResponse yourWebResponse = yourWebRequest.GetResponse();
string s = yourWebResponse.ToString();
// Get your response stream
using (var reponseStream = yourWebResponse.GetResponseStream())
{
// Build a reader to read your stream
using (var responseReader = new StreamReader(reponseStream, Encoding.UTF8))
{
// Get your result here
content = responseReader.ReadToEnd();
JObject json = JObject.Parse(content);
return Ok(json);
}
}
}
else
{
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
}
}
catch (Exception ex)
{
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
}
}