0

I'm trying to create a JSON/JSONP proxy for an external API. Here's my config so far:

server {
        listen          80;
        server_name     api.example.com;
        location /api/ {
                if ( $arg_callback ) {
                        echo_before_body '$arg_callback(';
                        echo_after_body ');';
                }
                if ($args ~ (.*)callback=[^&]*(.*)) {
                        set $args $1$2;
                }
                proxy_pass      http://api.external.com;
                rewrite ^/api/(.*)$ /external/api/$1?api_key=MY_KEY$is_args$args break;
        }
}

The proxy works just fine with no callback parameter, returning valid JSON. However, when I include the callback, it still returns only JSON without the callback wrapped around it. I have to strip the callback parameter before I call the external API, because it returns an error if the request contains unrecognized options. How can I strip the callback parameter out of the request (if it exists) before passing on the request but still use it to create the JSONP?

Ryan Kennedy
  • 203
  • 2
  • 11

1 Answers1

1

did you read if is evil?

it still returns only JSON without the wrapper.

what is the wrapper here for you? the first/the second if or both?

EDIT:

i'd do simple debugging to test, if your regex is valid (increase log-level to see whats happening)(solution is untested)

server {
        listen          80;
        server_name     api.example.com;
        location /api/ {


                rewrite ^/(.*)callback=[^&]*(.*)$ /testurl/$1/$2 last;  
        }

        location /testurl  {
           return 200;
        }


}

after this step i'd put in the 2nd IF - statement, and if that works, the first one finally

how does your url looks like with that callback-parameters?