7

I have the following response that should come from a MailChimp webhook URL.

This is the row BODY:

RAW BODY

    type=usub&fired_at=2015-07-23+17%3A19%3A34&data%5Baction%5D=unsub&data%5Breason%5D=manual&data%5Bid%5D=9383uy636&data%5Bemail%5D=youremail%40YOURDOMAIN.com&data%5Bemail_type%5D=html&data%5Bip_opt%5D=202.9.3.003&data%5Bweb_id%5D=404004&data%5Bmerges%5D%5BEMAIL%5D=YOUREMAIL%40YOURDOMAIN.com&data%5Bmerges%5D%5BFNAME%5D=NAME&data%5Bmerges%5D%5BLNAME%5D=LASTNAME&data%5Blist_id%5D=2288883
 FORM/POST PARAMETERS

fired_at: 2015-07-22 12:19:34
data[email]: YOUREMAIL@DOMAINNAME.com
data[id]: 56775409ta
data[web_id]: 09833944
data[merges][EMAIL]: YOUREMAIL@DOMAINNAME.com
type: unsub
data[list_id]: 99884hy372
data[merges][FNAME]: Name
data[ip_opt]: 202.0.9.3333
data[reason]: manual
data[email_type]: html
data[action]: unsub
data[merges][LNAME]: LastName

**QUERYSTRING key: a4483983hu473004884j0x**

HEADERS

Accept: */*
Total-Route-Time: 0
Host: requestb.in
Connection: close
Content-Length: 395
User-Agent: MailChimp.com
Connect-Time: 0
Via: 1.1 vegur
X-Request-Id: 6633d8-653e-4cea-884j-9933ju4773h
Content-Type: application/x-www-form-urlencoded

I have used @RequestParameter previously when working with Spring Controllers, however I am not sure how to get the data from the above response for e.g data[merges][FNAME]:

How can I also get the QueryString inside a Spring controller ? QUERYSTRING key: a4483983hu473004884j0x

//I have the following code to begin with
@RequestMapping("/unsubscribewebhook")
    public ZapJasonMessage unsubscribeWebHook(
    @RequestParam("key") String key,
    @RequestParam ("data[merges][FNAME]") String firstName
) {

}

I appreciate your help!

WowBow
  • 7,137
  • 17
  • 65
  • 103
  • removed the answer. Let me know if you find any solution :) – Amit.rk3 Jul 24 '15 at 16:19
  • MailChimp guys said, this data sent via webhook is formatted as HTTP POST data like a form submittion, and not JSON. This means that you should handle this information in the same way you would handle data submitted through a form on your web page. I am using Spring .. – WowBow Jul 24 '15 at 17:11
  • So passing request object in method argument in controller and doing request.getParameter should work – Amit.rk3 Jul 24 '15 at 17:30
  • I thought so .. but I tried to print out all the parameters but nothing comes. I guess we can't get form parameters from request.getParameters ... – WowBow Jul 24 '15 at 18:14

1 Answers1

6

You just need to inject WebRequest as a parameter into your controller POST method, and then ask for getParameter(), with a given parameter from the Mailchimp webhook.

e.g:

@RequestMapping(path="/MyUrl", method=RequestMethod.POST)
public ModelAndView process(WebRequest request){
    System.out.println(request.getParameter("type"));
    System.out.println(request.getParameter("data[merges][EMAIL]"));
    return new ModelAndView([YOURVIEWHERE]);
}

That's all...

Regards.

emeraldjava
  • 10,894
  • 26
  • 97
  • 170