I am trying to use following code to send request to sendgrid for adding emails to list, but i always keep getting 404 : Bad request.
def chttps = new HTTPBuilder('https://api.sendgrid.com/api/newsletter/lists/email/add.json?&api_user=myUser&api_key=myKey')
chttps.request( Method.POST, ContentType.JSON ) { req ->
headers.'Content-Type' = 'application/json'
body = [
list : 'testlist',
data : [email : '123Ex@exm.pl', name : '123Ex' ]
]
response.success = { resp, json ->
// response handling here
}
// handler for any failure status code:
}
Following is the error i get:
Bad Request. Stacktrace follows:
groovyx.net.http.HttpResponseException: Bad Request
at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:609)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:475)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:417)
at groovyx.net.http.HTTPBuilder.request(HTTPBuilder.java:366)
at com.farmfresh.brandywine.erp.SendGridService$$EOc3vKl8.addEmailsToRecipientList(SendGridService.groovy:16)
at com.farmfresh.brandywine.erp.CustomerController$_closure5$$EOc3pEvI.doCall(CustomerController.groovy:106)
at org.zkoss.zk.grails.web.ZKGrailsPageFilter.obtainContent(ZKGrailsPageFilter.java:238)
at org.zkoss.zk.grails.web.ZKGrailsPageFilter.doFilter(ZKGrailsPageFilter.java:189)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
can't find what's wrong. Please help.
EDIT1:
So i tried using following code, and it works fine, but still cant specify list of emails in data section:
def chttps = new HTTPBuilder('https://api.sendgrid.com/api/newsletter/lists/email/add.json?&api_user=myUser&api_key=myKey')
def dataa = "list=testlist&data=%7B%22email%22%3A%22examuttample%40gmail.com%22%2C%22name%22%3A%22uttam%22%7D"
chttps.post( body: dataa
) { resp ->
println "${resp}"
resp.headers.each {
println "${it.name} : ${it.value}"
}
println "${resp.data}"
println "http POST Success: ${resp.statusLine}"
}
For adding multiple emails in single request i tried putting following format:
data=%5B%7B%22email%22%3A+%22example1112%40gmail.com%22%2C%22name%22%3A+%22112example%22%7D%2C%7B%22email%22%3A+%22example2222%40gmail.com%22%2C%22name%22%3A+%22222example%22%7D%5D
//encode for :: [{"email": "example1112@gmail.com","name": "112example"},{"email": "example2222@gmail.com","name": "222example"}]
but i keep getting following exception, is there a way around this to add multiple emails.
groovyx.net.http.HttpResponseException: Internal Server Error
at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:642)
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:494)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1070)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:1044)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:506)
at groovyx.net.http.HTTPBuilder.post(HTTPBuilder.java:343)
at com.farmfresh.brandywine.erp.SendGridService$$EOc9RNxT.addEmailsToRecipientList(SendGridService.groovy:40)
at com.farmfresh.brandywine.erp.CustomerController$_closure5$$EOc9QqKy.doCall(CustomerController.groovy:109)
at org.zkoss.zk.grails.web.ZKGrailsPageFilter.obtainContent(ZKGrailsPageFilter.java:238)
at org.zkoss.zk.grails.web.ZKGrailsPageFilter.doFilter(ZKGrailsPageFilter.java:189)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)