0

URL encoding of parameter used in execute block

Greetings. Just cutting my teeth on YQL -- very powerful stuff!

I'm using an execute block in my Open Data Table Definition that looks something like this:

CODE
<execute>
<![CDATA[
var myRequest = y.rest('http://foo.com').query("location", location);
var data = myRequest.get().response;
response.object = data;
]]>
</execute>


The location parameter is a GPS coordinate, in a form like: "38.889722,-77.008889".

When I query with something like the following:

CODE
use "http://mydomain.com/yql/odt_def.xml";select * from odt_def where location="38.889722,-77.008889";


YQL replaces the comma with "%252c", as in:

CODE
http://foo.com?location=38.889722%252c-77.008889


The above query does not work; while either of the following would work fine:

CODE
http://foo.com?location=38.889722%2c-77.008889
http://foo.com?location=38.889722,-77.008889


I'm probably missing something basic, but didn't see anything in this forum.

Thank you,

Brian Felder

by
5 Replies
  • After posting, saw this post, indicating that there's an outstanding issue around encoding commas in URLs:

    http://developer.yahoo.net/forum/index.php...post&p=4532

    I came up with the workaround of passing in a Lat and Long, then concatenating them with a comma as the query element.

    Brian
    0
  • First, why use y.rest? The web service probably serves XML or JSON as output format, so you can stick with a plain y.query request. Second, the comma encoding issue seems to be solved. I'm using a web service that has the same lat,lon parameter and it works fine in both y.query and y.rest. Here's a working example:

    CODE
    use "http://yql.s3.amazonaws.com/query_or_rest.xml" as qor; select * from qor where location="38.889722,-77.008889" and method="query"
    use "http://yql.s3.amazonaws.com/query_or_rest.xml" as qor; select * from qor where location="38.889722,-77.008889" and method="rest"


    CODE
    <execute><![CDATA[
    var URI = "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml",
    YQ = null, // YQL Query (either y.query or y.rest)
    YQR = null; // YQL Query Result

    if ( method == "query" ) {
    URI = URI + '?query=' + location;
    YQ = y.query("select * from xml where url=\"" + URI + "\"");
    YQR = ( YQ.results ) ? YQ.results.current_observation : null;
    }
    else if ( method == "rest" ) {
    YQ = y.rest(URI).query("query",location).get();
    YQR = ( YQ.response ) ? YQ.response : null;
    }

    response.object = YQR;
    ]]></execute>
    0
  • Good to see the two approaches possible for calling a restful web service. I can see that using y.query rather than y.rest allows you to filter based on the results, rather than only on input parameters to the web service.

    Thanks for your help!

    Brian

    QUOTE (Ivo @ Jun 20 2009, 12:48 PM) <{POST_SNAPBACK}>
    First, why use y.rest? The web service probably serves XML or JSON as output format, so you can stick with a plain y.query request. Second, the comma encoding issue seems to be solved. I'm using a web service that has the same lat,lon parameter and it works fine in both y.query and y.rest. Here's a working example:

    CODE
    use "http://yql.s3.amazonaws.com/query_or_rest.xml" as qor; select * from qor where location="38.889722,-77.008889" and method="query"
    use "http://yql.s3.amazonaws.com/query_or_rest.xml" as qor; select * from qor where location="38.889722,-77.008889" and method="rest"


    CODE
    <execute><![CDATA[
    var URI = "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml",
    YQ = null, // YQL Query (either y.query or y.rest)
    YQR = null; // YQL Query Result

    if ( method == "query" ) {
    URI = URI + '?query=' + location;
    YQ = y.query("select * from xml where url=\"" + URI + "\"");
    YQR = ( YQ.results ) ? YQ.results.current_observation : null;
    }
    else if ( method == "rest" ) {
    YQ = y.rest(URI).query("query",location).get();
    YQR = ( YQ.response ) ? YQ.response : null;
    }

    response.object = YQR;
    ]]></execute>
    0
  • QUOTE (crowdvark @ Jun 19 2009, 10:44 PM) <{POST_SNAPBACK}>
    Greetings. Just cutting my teeth on YQL -- very powerful stuff!

    I'm using an execute block in my Open Data Table Definition that looks something like this:

    CODE
    <execute>
    <![CDATA[
    var myRequest = y.rest('http://foo.com').query("location", location);
    var data = myRequest.get().response;
    response.object = data;
    ]]>
    </execute>


    The location parameter is a GPS coordinate, in a form like: "38.889722,-77.008889".

    When I query with something like the following:

    CODE
    use "http://mydomain.com/yql/odt_def.xml";select * from odt_def where location="38.889722,-77.008889";


    YQL replaces the comma with "%252c", as in:

    CODE
    http://foo.com?location=38.889722%252c-77.008889


    The above query does not work; while either of the following would work fine:

    CODE
    http://foo.com?location=38.889722%2c-77.008889
    http://foo.com?location=38.889722,-77.008889


    I'm probably missing something basic, but didn't see anything in this forum.

    Thank you,

    Brian Felder



    Just taking a quick guess here. Is your location declared as a query paramType? i.e paramType="query"?
    If so then that would be the problem. It should ideally be variable.

    Now if you use the query parameter then its already encoded when you get the location in the execute so in essence it gets double encoded when its passed to the query(..) method.

    -- Nagesh
    0
  • Yes, I was using paramType=query. paramType=variable seems to resolve the issue.

    Brian

    QUOTE (Nagesh Susarla @ Jun 20 2009, 11:34 PM) <{POST_SNAPBACK}>
    Just taking a quick guess here. Is your location declared as a query paramType? i.e paramType="query"?
    If so then that would be the problem. It should ideally be variable.

    Now if you use the query parameter then its already encoded when you get the location in the execute so in essence it gets double encoded when its passed to the query(..) method.

    -- Nagesh
    0

Recent Posts

in YQL