MailChimp: Why is PUT method with javascript returning "Use PUT to insert or update list members"
Asked Answered
S

3

8

I'm trying to update one of my subscriber's status using mailchimp API 3.0, Meteor and javascript.

Here is my js code I'm using:

request({
  uri,
  list_id,
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'apikey (my api key)'
  },
  json,
}, function(err, res, body) {
  if (err) {
    return console.log("err:", err);
  }
  console.log("connection succeed");
  console.log("res: ", res.body);
  console.log("body: ", body);
});

with

  uri = "https://us15.api.mailchimp.com/3.0/lists/" + (id of my list) + "/members/" + (md5 of my user mail);

and

  json = {
    "email_address": (user mail as a string),
    "status": "unsubscribed"
  };

But I always have the same output:

I20181204-18:42:12.714(8)? title: 'Member Exists', I20181204-18:42:12.714(8)? status: 400, I20181204-18:42:12.714(8)? detail: '(user mail adress) is already a list member. Use PUT to insert or update list members.'

But I am using PUT already... The request works with POST if it's the first time I add the user. But now I can't update my user status... Is something wrong with my request or with the way I use the API? Thanks in advance.

EDIT 1 -> trying with GET doesn't work. The request itself is correct but it has no effect on my subscriber's status. So I still need to make PUT work.

Sedan answered 4/12, 2018 at 10:54 Comment(0)
S
7

After looking at the official doc in the "Edit" tab, I found the answer!

The json required another mandatory parameter and should look like this:

  json = {
    "email_address":  (user mail as a string),
    "status_if_new": "unsubscribed",
    "status": "unsubscribed"
  };
Sedan answered 5/12, 2018 at 7:16 Comment(0)
J
3

I know that this is an older question but I just wanted to add something in the event that it helps someone. I was having a similar issue intermittently with most of my PUT requests working as expected and some not. It took me a while but eventually I figured out that some of my email addresses had spaces at the end. This error would result. Trimming the addresses before doing anything resolved my issue.

Jointer answered 26/12, 2021 at 6:57 Comment(0)
D
0

I ran into the same issue and discovered that I had forgotten to lowercase the email before computing the hash. This is how the Mailchimp API validates the hashes, which was what led to the rather cryptic error message.

Lower casing the email before running it through the hash function solved my problem.

Divert answered 1/3 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.