soo happy 的节奏,不过,现在用不了,只能口水ing...
嗯嗯嗯,这就是两年前,讨论线索最后推荐的 cosocket 数据流思路哪!
又参考了以下代码:
HttpLuaModule - Nginx Community
http://wiki.nginx.org/HttpLuaModule#ngx.location.capture
...
res = ngx.location.capture(
'/foo/bar',
{ method = ngx.HTTP_POST, body = 'hello, world' }
)
Posting to
goo.gl from Android - Stack Overflow
http://stackoverflow.com/questions/6052156/posting-to-goo-gl-from-android
...
post.setEntity(new StringEntity("{\"longUrl\": \"
http://www.google.com/\"}"));
...
post.setHeader("Content-Type", "application/json");
解决了问题 ;-)
配置中追加:
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;
}
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)
测试成功:
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"
}
so happy now,
俺将合并这个小故事到俺的乱入手册中,
彻底放弃 luacurl 的方案.
thanx for all.
> Regards,
> -agentzh