在邮件中使用groovy httpbuilder发布params

时间:2015-03-28 03:54:19

标签: groovy mailgun httpbuilder

我正在尝试调用mailgun发送消息api。如果我将参数与url -

一起传递,则以下代码有效
String targetUrl = "https://api.mailgun.net/v2/my_domain_name/messages?from=fromAddress&to=toAddress&subject=sub&text=random+message" 

但是当我尝试在体内添加这些参数时,它就不起作用了。我一直收到来自mailgun的错误请求。 -

String targetUrl = "https://api.mailgun.net/v2/my_domain_name/messages
body = [from:"fromAddress", to:"toAddress", subject:"sub", text:"random message"]

以下是完整代码 -

def sendEmail(String mailBody, String sub, String toIds) {
    String targetUrl = "https://api.mailgun.net/v2/my_domain_name/messages"
    def http = new HTTPBuilder(targetUrl)

    http.request( groovyx.net.http.Method.POST, groovyx.net.http.ContentType.JSON) {

        body = [from:"fromAddress", to:"toAddress", subject:"sub", text:"random message"]

        headers = ['Authorization':"Basic " + "api:my_api_key".bytes.encodeBase64().toString()]

        response.success = { resp, reader ->
            println "valid response: " + reader
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:4)

您的代码无效,因为您告诉HTTPBuilder请求的参数是以JSON编码的

  

http.request(Method.POST, ContentType.JSON)

这样您就可以在JSON中对请求正文中的参数进行编码,而mailgun API期望它们为Content-Type: application/x-www-form-urlencoded

curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v2/YOUR_DOMAIN_NAME/messages \
    -F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
    -F to=YOU@YOUR_DOMAIN_NAME \
    -F to=bar@example.com \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomness!'

您可以使用targetURL http://echo.httpkit.com来查看它,它以JSON格式返回HTTP请求。

这样做,它产生:

{
    "body": "\"from\":\"fromAddress\",\"to\":\"toAddress\",\"subject\":\"sub\",\"text\":\"random message\"}",
"docs": "http://httpkit.com/echo",
[...]

因此,您必须使用ContentType.URLENC代替ContentType.JSON(并且可选地使用Accept标头application/json类型):

import groovy.json.*

import groovyx.net.*
import groovyx.net.http.*

import static groovy.json.JsonOutput.*

import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*

String targetUrl = 'http://echo.httpkit.com'
def http = new HTTPBuilder(targetUrl)

http.request(POST) {
    uri.path = '/'
    requestContentType = URLENC

    body = [from: 'fromAddress', to: 'toAddress', subject: 'sub', text: 'random message']

    headers.'Authorization' = "Basic " + "api:my_api_key".bytes.encodeBase64().toString()
    headers.'Accept' = 'application/json'

    response.success = { resp, json ->
        println prettyPrint(toJson(json))
    }
}

产生这个&#34; echo&#34;响应(即发送带有urlencoded参数的POST请求):

{
    "body": "from=fromAddress&to=toAddress&subject=sub&text=random+message",
    "docs": "http://httpkit.com/echo",
    "headers": {
        "accept": "application/json",
        "authorization": "Basic YXBpOm15X2FwaV9rZXk=",
        "content-length": "61",
        "content-type": "application/x-www-form-urlencoded",
        "host": "echo.httpkit.com",
        "x-forwarded-for": "195.235.15.200"
    },
    "ip": "127.0.0.1",
    "method": "POST",
    "path": {
        "name": "/",
        "params": {

        },
        "query": ""
    },
    "powered-by": "http://httpkit.com",
    "uri": "/"
}