Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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?

share|improve this answer
    
I'd read if is evil but went back and reread it. I'm not sure I understand your last sentence, could you perhaps reword it or be more specific? –  Ryan Muller Sep 9 '13 at 13:25
    
@RyanMuller: see my edit –  that guy from over there Sep 9 '13 at 15:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.