从grails发送邮件请求到mailchimp 2.0

时间:2014-08-10 09:27:11

标签: grails mailchimp http-status-code-302 httpbuilder

目前,我正在将我的grails项目与mailchimp 2.0 API集成(我们目前使用的是grails-mailchimp插件的1.2版本) 当我将带有json数据的post请求发送到mailchimp端点时,我总是收到此错误" HTTP / 1.1 302暂时移动"。我该如何解决?

这是我使用HTTPBuilder的代码

def twitter = new RESTClient( "https://us4.api.mailchimp.com/2.0/lists/member-info.json")
def resp = twitter.post(
        path : '/',
        body : body1,
        requestContentType : ContentType.JSON )

assert resp.status == 200

我的记录:

http.RESTClient POST https://us4.api.mailchimp.com/
conn.BasicClientConnectionManager Get connection for route {s}->https://us4.api.mailchimp.com
conn.DefaultClientConnectionOperator Connecting to us4.api.mailchimp.com:443
protocol.RequestAddCookies CookieSpec selected: best-match
protocol.RequestAuthCache Auth cache not set in the context
protocol.RequestTargetAuthentication Target auth state: UNCHALLENGED
protocol.RequestProxyAuthentication Proxy auth state: UNCHALLENGED
client.DefaultHttpClient Attempt 1 to execute request
conn.DefaultClientConnection Sending request: POST / HTTP/1.1
http.wire >> "POST / HTTP/1.1[\r][\n]"
http.wire >> "Accept: */*[\r][\n]"
http.wire >> "Content-Length: 106[\r][\n]"
http.wire >> "Content-Type: application/json[\r][\n]"
http.wire >> "Host: us4.api.mailchimp.com[\r][\n]"
http.wire >> "Connection: Keep-Alive[\r][\n]"
http.wire >> "[\r][\n]"
http.headers >> POST / HTTP/1.1
http.headers >> Accept: */*
http.headers >> Content-Length: 106
http.headers >> Content-Type: application/json
http.headers >> Host: us4.api.mailchimp.com
http.headers >> Connection: Keep-Alive
http.wire >> "{"id":"xxx","emails":[{"email":"xxx"}],"apikey":"xxx-us4"}"
http.wire << "HTTP/1.1 302 Moved Temporarily[\r][\n]"
http.wire << "Server: nginx[\r][\n]"
http.wire << "Content-Type: text/html; charset=UTF-8[\r][\n]"
http.wire << "Content-Length: 1[\r][\n]"
http.wire << "Location: http://apidocs.mailchimp.com/[\r][\n]"
http.wire << "Date: Sun, 10 Aug 2014 09:22:24 GMT[\r][\n]"
http.wire << "Connection: keep-alive[\r][\n]"
http.wire << "[\r][\n]"
conn.DefaultClientConnection Receiving response: HTTP/1.1 302 Moved Temporarily
http.headers << HTTP/1.1 302 Moved Temporarily
http.headers << Server: nginx
http.headers << Content-Type: text/html; charset=UTF-8
http.headers << Content-Length: 1
http.headers << Location: http://apidocs.mailchimp.com/
http.headers << Date: Sun, 10 Aug 2014 09:22:24 GMT
http.headers << Connection: keep-alive
client.DefaultHttpClient Connection can be kept alive indefinitely
http.RESTClient Response code: 302; found handler: org.codehaus.groovy.runtime.MethodClosure@30198ecc
http.RESTClient Parsing response as: text/html
http.wire << "[\n]"
conn.BasicClientConnectionManager Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@5dee0bbf
conn.BasicClientConnectionManager Connection can be kept alive indefinitely
http.RESTClient Parsed data to instance of: class groovy.util.slurpersupport.NodeChild

好像我发送的是http,而不是https(因为代码302暂时只为HTTP协议移动),不是吗?如果是,我该如何解决?

1 个答案:

答案 0 :(得分:1)

最后我可以找到一种发送到mailchimp的方法,它只是关注httpbuilder tutorial

    def emails = []
    emails.add(['email': email_address])
    def body1 = ['id' : id, 'emails' : emails, 'apikey': apiKey]
    def http = new HTTPBuilder("https://us4.api.mailchimp.com/2.0/lists/member-info.json")
    http.request(Method.POST, ContentType.JSON ) { req ->
        body = body1
        response.success = { resp, json ->
            assert resp.statusLine.statusCode == 200
            println json
        }
    }