Skip to content

Instantly share code, notes, and snippets.

@ZoomQuiet
Created August 11, 2014 10:13
Show Gist options
  • Select an option

  • Save ZoomQuiet/065f6688b8982f101ddb to your computer and use it in GitHub Desktop.

Select an option

Save ZoomQuiet/065f6688b8982f101ddb to your computer and use it in GitHub Desktop.
openresty cosocket for googleapis.com/urlshortener/v1/url
curl https://www.googleapis.com/urlshortener/v1/url \
-H 'Content-Type: application/json' \
-d '{"longUrl": "http://www.google.com/"}'
@ZoomQuiet

Copy link
Copy Markdown
Author

base suggest:

openresty/lua-nginx-module#178 (comment)

and:
HttpLuaModule - Nginx Community

...
res = ngx.location.capture(
    '/foo/bar',
    { method = ngx.HTTP_POST, body = 'hello, world' }
)

Posting to goo.gl from Android - Stack Overflow

...
post.setEntity(new StringEntity("{\"longUrl\": \"http://www.google.com/\"}"));
...
post.setHeader("Content-Type", "application/json");

i fixed all ;-)

append in nginx.conf:

location /googleapis {
    internal;
    rewrite ^/googleapis(.*) $1 break;  
    # to disable gzip compression on the backend
    proxy_set_header Accept-Encoding '';
    proxy_set_header Content-Type 'application/json';
    #proxy_set_header contentType 'application/x-www-form-urlencoded';
    proxy_pass https://www.googleapis.com;
}

fixed in lua :

SURI = "https://www.googleapis.com/urlshortener/v1/url"
ngx.say("uri\t", URI)
--DATA = "longUrl=" .. URI
--JSON = '{"longUrl"="' .. URI .. '"}'
local cjson = require "cjson"
JSON = cjson.encode({longUrl=URI})
ngx.say("JSON\t", JSON)

-- search for "openresty" in google over https:
local res = ngx.location.capture(
    "/googleapis/urlshortener/v1/url",
    { method = ngx.HTTP_POST,
        body = JSON }
    )

if res.status ~= 200 then
    ngx.say("failed to query google: ", res.status, ": ", res.body)
    return
end

-- here we just forward the Google search result page intact:
ngx.header["Content-Type"] = "application/json; charset=UTF-8"
ngx.say(res.body)

testing from China inside GFW:

$ curl -d "uri=https://docs.google.com/forms/d/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU/viewform" 23.239.0.15:10080/=/goo
request_method: POST
args.uri:   https://docs.google.com/forms/d/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU/viewform
uri https://docs.google.com/forms/d/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU/viewform
JSON    {"longUrl":"https:\/\/docs.google.com\/forms\/d\/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU\/viewform"}
{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/fiAfLo",
 "longUrl": "https://docs.google.com/forms/d/1TBJNar54XKhMu_nRaES5ByPv0078hh0GV0sD5JPCZGU/viewform"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment