locked
How do I call a WCF Service Operation from iOS? RRS feed

  • Question

  • This is a repost from a StackOverflow question.  Google this post's title and it'll probably come up; I apparently can't post links here until my account is verified.  I'll figure out how to do that and then come back and edit this.

    I've got a WCF Data Service set up with a ServiceOperation tied to a stored procedure in my DB (SQL Server).  I want to invoke it from iOS.  It's marked as a WebInvoke(Method = "POST").  I post to it from iOS and just get an error "Bad Request - Error in query syntax." wrapped in JSON.

    My ServiceOperation looks a lot like this:

    public IQueryable<Foo> GetFoos(int param1, DateTime param2, string param3)
    {
     
    return CurrentDataSource.GetFoos(param1, param2, param3).AsQueryable();
    }

    Thursday, September 27, 2012 8:10 PM

Answers

All replies

  • Could you please provide a fiddler trace? Thanks.

    I'm wondering if the iOS client doesn't properly handle POST service operations. Maybe you could try making the service operation a WebGet instead, and see what happens?

    -Ian

    Thursday, September 27, 2012 9:32 PM
  • I can't post a fiddler trace because I'm running this from either an iOS device or the iOS Simulator, but I can put the headers and parameters that I'm sending.  I'm setting the HTTP method of the request to POST.

    Headers:

    • key field for API key
    • application/json;charset=utf-8 for both Content-Type and accept
    • hardware and software descriptions and the client app version for logging purposes server-side

    Request body: {"param1":42,"param3":"testString","param2":"datetime'2012-44-27T14:44:06'"}

    I've tried other DateTime formats to no avail.

    I don't think the POST is the problem, because I have a plain WCF service I'm calling with the same WCF client with no problems.

    Thursday, September 27, 2012 9:45 PM
  • Service operations don't support passing parameters in the body. You need to pass all the parameters in the URL as query options:

    ~/ServiceOperation?param1=42&param3='testString'&param2=datetime'2012-44-27T14:44:06'

    (Note that the date seems wrong, I doubt there's a culture in which there are 44 months, let alone the fact that the date time literal requires UTC).

    Only Actions which were added in OData V3 support passing parameters in the payload (http://msdn.microsoft.com/en-us/library/hh859851(v=vs.103).aspx, http://blogs.msdn.com/b/astoriateam/archive/2011/10/17/actions-in-wcf-data-services.aspx).

    Thanks,


    Vitek Karas [MSFT]


    Friday, September 28, 2012 7:50 AM
    Moderator
  • Thanks, this works.  I had noticed that the date was wrong; I had a typo in my date format string.
    Friday, September 28, 2012 2:40 PM