CouchDB
  1. CouchDB
  2. COUCHDB-1017

HTTP Rewrite Handler: Rewriting strings in queries

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.0.1
    • Fix Version/s: None
    • Component/s: HTTP Interface
    • Labels:
      None
    • Skill Level:
      Regular Contributors Level (Easy to Medium)

      Description

      I have this rewrite:
      {
      "from": "/u/:key",
      "to": "_list/pouch/user-items",
      "method": "GET",
      "query":

      {"key": ":key"}

      }
      ..where the view returns:

      {"error":"bad_request","reason":"invalid UTF-8 JSON"}

      . The view query looks like this ?key=somekey, but for the view to succeed "somekey" need to be in double quotes.

      Maybe one could improve the rewriter's behavior for rewriting strings in queries?

      Here is one workaround:
      {
      "from": "/u/:startkey",
      "to": "_list/pouch/user-items",
      "method": "GET",
      "query": {
      "startkey": [":startkey"],
      "endkey": [":startkey", {}]
      }
      },

        Activity

        Hide
        Markus Barchfeld added a comment -

        Thanks for your comment, Gabriel. It made me look into #1074, where everything is explained.

        My use case was to rewrite an URL with year and month into startkey and endkey parameters.
        E.g the incoming URL

        "availabledays/2010/1"

        with the rewrite rule

        {
        "from": "/availabledays/:year/:month",
        "to": "_list/available/measurements",
        "query": {
        "group_level": "3",
        "startkey": [ ":year", ":month" ],
        "endkey": [ ":year",":month", {}]
        },
        "formats":

        { "year": "int", "month": "int" }

        },

        is transformed into

        _list/available/measurements?startkey=%5B2010%2C1%5D&endkey=%5B2010%2C1%2C%7B%7D%5D&group_level=3&year=2010&month=1

        Which is perfect. Or at least nearly Any way to get rid of the year and month query parameters in the rewritten rule?

        Show
        Markus Barchfeld added a comment - Thanks for your comment, Gabriel. It made me look into #1074, where everything is explained. My use case was to rewrite an URL with year and month into startkey and endkey parameters. E.g the incoming URL "availabledays/2010/1" with the rewrite rule { "from": "/availabledays/:year/:month", "to": "_list/available/measurements", "query": { "group_level": "3", "startkey": [ ":year", ":month" ], "endkey": [ ":year",":month", {}] }, "formats": { "year": "int", "month": "int" } }, is transformed into _list/available/measurements?startkey=%5B2010%2C1%5D&endkey=%5B2010%2C1%2C%7B%7D%5D&group_level=3&year=2010&month=1 Which is perfect. Or at least nearly Any way to get rid of the year and month query parameters in the rewritten rule?
        Hide
        Gabriel Farrell added a comment - - edited

        Benoit, works for me. Which commit fixed this?

        Markus, have you tried this in your rewrites: "startkey": [":year", ":month"].

        Show
        Gabriel Farrell added a comment - - edited Benoit, works for me. Which commit fixed this? Markus, have you tried this in your rewrites: "startkey": [":year", ":month"] .
        Hide
        Markus Barchfeld added a comment -

        Thanks for looking into it. However, I have quite the reverse case: calling a view with a hierarchical key does not work with quotes.

        For example startkey=[2008,08] works fine but startkey="[2008,08]" does not work at all.

        Is there a way how I could avoid setting the quotes? Or just any workaround how I could rewrite the above example from "myview/2008/08"?

        Thanks

        Show
        Markus Barchfeld added a comment - Thanks for looking into it. However, I have quite the reverse case: calling a view with a hierarchical key does not work with quotes. For example startkey= [2008,08] works fine but startkey=" [2008,08] " does not work at all. Is there a way how I could avoid setting the quotes? Or just any workaround how I could rewrite the above example from "myview/2008/08"? Thanks
        Hide
        Benoit Chesneau added a comment -

        should be fixed in 1.1. Let me know.

        Show
        Benoit Chesneau added a comment - should be fixed in 1.1. Let me know.
        Hide
        Marcos Zanona added a comment -

        Any news on this subject?
        I am having the same issue

        Show
        Marcos Zanona added a comment - Any news on this subject? I am having the same issue
        Hide
        Michel Legnered added a comment -

        Here is the full debug log for the request:

        [info] [<0.32.0>] Apache CouchDB has started on http://127.0.0.1:80/
        [debug] [<0.104.0>] 'GET' /inventoria/_design/inventoria/_rewrite/u/miche

        {1,1}
        Headers: [{'Accept',"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"},
        {'Accept-Charset',"ISO-8859-1,utf-8;q=0.7,*;q=0.3"},
        {'Accept-Encoding',"gzip,deflate,sdch"},
        {'Accept-Language',"en-US,en;q=0.8"},
        {'Cache-Control',"max-age=0"},
        {'Connection',"keep-alive"},
        {'Host',"127.0.0.1"},
        {'User-Agent',"Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10"}]
        [debug] [<0.104.0>] OAuth Params: []
        [debug] [<0.104.0>] rewrite to "/inventoria/_design/inventoria/_list/pouch/user-items?key=miche"

        [debug] [<0.104.0>] 'GET' /inventoria/_design/inventoria/_list/pouch/user-items?key=miche {1,1}

        Headers: [

        {'Accept',"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"}

        ,

        {'Accept-Charset',"ISO-8859-1,utf-8;q=0.7,*;q=0.3"}

        ,

        {'Accept-Encoding',"gzip,deflate,sdch"}

        ,

        {'Accept-Language',"en-US,en;q=0.8"}

        ,

        {'Cache-Control',"max-age=0"}

        ,

        {'Connection',"keep-alive"}

        ,

        {'Host',"127.0.0.1"}

        ,

        {'User-Agent',"Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10"}

        ]
        [debug] [<0.104.0>] OAuth Params: [

        {"key","miche"}

        ]
        [debug] [<0.101.0>] Spawning new group server for view group _design/inventoria in database inventoria.
        [debug] [<0.104.0>] request_group

        {Pid, Seq}

        {<0.127.0>,233}

        [error] [<0.104.0>] attempted upload of invalid JSON (set log_level to debug to log it)
        [debug] [<0.104.0>] Invalid JSON: "miche"
        [info] [<0.104.0>] 127.0.0.1 - - 'GET' /inventoria/_design/inventoria/_list/pouch/user-items?key=miche 400
        [debug] [<0.83.0>] New task status for inventoria _design/inventoria: Finishing.
        [debug] [<0.104.0>] httpd 400 error response:

        {"error":"bad_request","reason":"invalid UTF-8 JSON"}
        Show
        Michel Legnered added a comment - Here is the full debug log for the request: [info] [<0.32.0>] Apache CouchDB has started on http://127.0.0.1:80/ [debug] [<0.104.0>] 'GET' /inventoria/_design/inventoria/_rewrite/u/miche {1,1} Headers: [{'Accept',"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"}, {'Accept-Charset',"ISO-8859-1,utf-8;q=0.7,*;q=0.3"}, {'Accept-Encoding',"gzip,deflate,sdch"}, {'Accept-Language',"en-US,en;q=0.8"}, {'Cache-Control',"max-age=0"}, {'Connection',"keep-alive"}, {'Host',"127.0.0.1"}, {'User-Agent',"Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10"}] [debug] [<0.104.0>] OAuth Params: [] [debug] [<0.104.0>] rewrite to "/inventoria/_design/inventoria/_list/pouch/user-items?key=miche" [debug] [<0.104.0>] 'GET' /inventoria/_design/inventoria/_list/pouch/user-items?key=miche {1,1} Headers: [ {'Accept',"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"} , {'Accept-Charset',"ISO-8859-1,utf-8;q=0.7,*;q=0.3"} , {'Accept-Encoding',"gzip,deflate,sdch"} , {'Accept-Language',"en-US,en;q=0.8"} , {'Cache-Control',"max-age=0"} , {'Connection',"keep-alive"} , {'Host',"127.0.0.1"} , {'User-Agent',"Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10"} ] [debug] [<0.104.0>] OAuth Params: [ {"key","miche"} ] [debug] [<0.101.0>] Spawning new group server for view group _design/inventoria in database inventoria. [debug] [<0.104.0>] request_group {Pid, Seq} {<0.127.0>,233} [error] [<0.104.0>] attempted upload of invalid JSON (set log_level to debug to log it) [debug] [<0.104.0>] Invalid JSON: "miche" [info] [<0.104.0>] 127.0.0.1 - - 'GET' /inventoria/_design/inventoria/_list/pouch/user-items?key=miche 400 [debug] [<0.83.0>] New task status for inventoria _design/inventoria: Finishing. [debug] [<0.104.0>] httpd 400 error response: {"error":"bad_request","reason":"invalid UTF-8 JSON"}
        Hide
        Benoit Chesneau added a comment -

        what is the source url ?, can you paste couchdb log here too ?

        Show
        Benoit Chesneau added a comment - what is the source url ?, can you paste couchdb log here too ?

          People

          • Assignee:
            Benoit Chesneau
            Reporter:
            Michel Legnered
          • Votes:
            3 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:

              Development