I am trying to return a list of changed members from the mailchimp API, which returns the following (correct) json if I use curl/Postman:
{
"members": [
{
"id": "187fhdy650b10e8d43552b6c3672368212",
"email_address": "example@email.co.uk",
"unique_email_id": "2edbhgy73836",
"email_type": "html",
"status": "unsubscribed",
"merge_fields": {
"FNAME": "",
"LNAME": "",
"MMERGE5": "",
//etc....
I want to return this object using a Node application, so am using node-fetch
as follows:
fetch(`https://us5.api.mailchimp.com/3.0/lists/<listid>/members?since_last_changed=2016-10-18T23:10:18+00:00`, {headers: authHeader})
.then((response) => {
if (response.ok) {
res.status(response.status).send(response)
} else {
res.status(response.status).send(response)
}
}).catch((error) => {
res.status(500).send(error.message)
})})
This returns a completely different json structure, which to me suggests it is mid-way through receiving the data and the fetch promise shouldn't have resolved. It's quite large, so I'm including what I see as the key bits of information:
{
"_opts": {},
"_chunkSize": 16384,
"_readableState": {
"objectMode": false,
"highWaterMark": 16384,
"buffer": [],
"length": 0,
"pipes": null,
"pipesCount": 0,
"flowing": null,
"ended": false,
"endEmitted": false,
"reading": false,
"sync": false,
"needReadable": true,
"emittedReadable": false,
"readableListening": false,
"resumeScheduled": false,
"defaultEncoding": "utf8",
"ranOut": false,
"awaitDrain": 0,
"readingMore": false,
"decoder": null,
"encoding": null
},
"readable": true,
"domain": null,
"_events": {
"end": [
null,
null
]
},
"_eventsCount": 7,
"_writableState": {
"objectMode": false,
"highWaterMark": 16384,
"needDrain": false,
"ending": true,
"ended": true,
"finished": false,
"decodeStrings": true,
"defaultEncoding": "utf8",
"length": 2674,
"writing": true,
"corked": 0,
"sync": false,
"bufferProcessing": false,
"writelen": 2674,
"bufferedRequest": null,
"lastBufferedRequest": null,
"pendingcb": 1,
"prefinished": false,
"errorEmitted": false,
"bufferedRequestCount": 0,
"corkedRequestsFree": {
"next": null,
"entry": null
}
},
"writable": false,
"allowHalfOpen": true,
"_transformState": {
"needTransform": false,
"transforming": true,
"writechunk": {
"type": "Buffer",
"data": [
31,
139,
8,
0,
0,
0,
0,
0,
198,....
I've tried parsing this in different ways, such as reading the reponse first:
.then((response) => {
response.text()})
.then((response) => {
res.status(200).send(response)
})
which returns an undefined object. And also parsing as json:
.then((response) => {
res.status(200).send(response.json())
})
which returns an empty object {}
Is there anything more I can try with my fetch call?
fetch()
returns a promise that resolves to a response object, which in turn has various methods ( .text()
, .json()
) that return promises that resolve to the body of the response.
So try this:
fetch(`https://us5.api.mailchimp.com/3.0/lists/<listid>/members?since_last_changed=2016-10-18T23:10:18+00:00`, {headers: authHeader})
.then(response => {
res.status(response.status);
return response.json();
}).then(body => {
res.send(body);
}).catch(error => {
res.status(500).send(error.message)
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.