从调用卷曲的.NET Web API [英] Calling .Net Web API from cURL
问题描述
我写在.NET中身份验证提供一个REST服务,使其更容易从PHP和iOS调用。我用了MVC的Web API库,这一切工作正常,我可以调用从浏览器的方法等。
I am writing an authentication provider in .Net as a REST service to make it easier to call from PHP and iOS. I've used the MVC Web API libraries and it all works fine, I can call methods from the browser etc.
但是,当我尝试利用卷曲打电话给我POST方法的卷曲-d什么东西=hxxp:// myendpoint / API 与-d邮件正文中传递的数据不间preTED以匹配我的Web API类的方法和我看不出如何获得这种形式的类型字段的访问。
BUT when I try and call my POST method from cURL using curl -d "something=something" hxxp://myendpoint/api the data passed in the message body with -d is not interpreted to match methods in my Web API class and I cannot see how to gain access to this form type fields.
我不能改变卷曲的呼叫,因为它是从第三方库来了,似乎是正确的,但尽管我明白,网页API设计只匹配URI参数的行为,我不明白为什么我不能从Request对象访问表单数据(或者也许我可以吗?)
I cannot change the curl call because it is coming from a 3rd-party library and seems to be correct but although I understand that Web API by design only matches uri parameters to actions, I don't understand why I can't access the form data from the Request object (or maybe I can?)
[HttpPost] public AccessToken Post() {} // Matches but can't access form data
[HttpPost] public AccessToken Post2(string something) {} // Doesn't match, no parameter in curl uri querystring
[HttpPost] public AccessToken Post3(ModelWithOnePropertyCalledSomething data) {} //Matches but can't access form data
这是真的东西,是不可能与Web API?我必须编写自定义动作选择类?
Is this really something that is not possible with the Web API? Do I have to write custom action selector classes?
推荐答案
我发现我所需要的,它是这样的:
I've found what I needed, it is something like this:
[HttpPost]
public AccessToken Post2()
{
var data = Request.Content.ReadAsFormDataAsync();
data.Wait();
var form = data.Result;
return new AccessToken() { Value = "" };
}
基本上,网络API框架的要求,显然都是异步这是很好的,但它需要code的几行,使其工作。我猜我需要等待数据,因为ReadAsFormDataAsync可能没有通过我达到code的下一行的时间内完成。我还需要检查张贴的类型等,以避免例外,我不希望他们。
Basically, the request and apparently all of the web api framework is asynchronous which is fine but it requires a few more lines of code to make it work. I'm guessing I need to wait for data since ReadAsFormDataAsync might not have completed by the time I reach the next line of code. I also need to check for the posted type etc. to avoid exceptions where I don't want them.
这篇关于从调用卷曲的.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!