Reputation: 1545
UPDATE: I have updated my code with the recommended answer, and am now getting a different error than I originally did, explained below.
I am using Meteor js http package and I am attempting to send a POST request to the Constant Contact API. I am trying to use the data
option to pass in a JSON-able object to stringify and use as the HTTP request body. I am getting a 400 error response from Constant Contact. Using the Constant Contact API tester I was able to successfully get a 201 response and Add a contact. The Json I have here is the same that I used in the tester but I get the following error back.
{ [Error: failed [400] [{"error_key":"query.param.invalid","error_message":"The query parameter status is not supported."},{"error_key":"query.param.invalid","error_message":"The query parameter limit is not supported."}]]
Here is my code below.
var data = {
"addresses": [
{
"address_type": "BUSINESS",
"city": "Belleville",
"country_code": "CA",
"line1": "47 Shawmut Ave.",
"line2": "Suite 404",
"postal_code": "K8b 5W6",
"state_code": "ON"
}
],
"lists": [
{
"id": "1395617465"
}
],
"cell_phone": "555-555-5555",
"company_name": "System Optimzations",
"confirmed": false,
"email_addresses": [
{
"email_address": "[email protected]"
}
],
"fax": "555-555-5555",
"first_name": "Ronald",
"home_phone": "555-555-5555",
"job_title": "Systems Analyst 3",
"last_name": "Martone",
"prefix_name": "Mr.",
"work_phone": "555-555-5555"
};
HTTP.post('https://api.constantcontact.com/v2/contacts?status=ALL&limit=50&api_key=<random-key>', {
headers: {
'Authorization': 'Bearer <random-token>',
'Content-Type': 'application/json'
},
data: JSON.stringify(data)
}, function (error, response) {
if ( error ) {
console.log( error );
} else {
console.log(response);
}
});
Upvotes: 3
Views: 2149
Reputation: 18393
The correct url for POST is
https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=<api-key>
not
https://api.constantcontact.com/v2/contacts?status=ALL&limit=50&api_key=<random-key>
Please see docs here: https://constantcontact.mashery.com/io-docs Contact methods section.
Upvotes: 2
Reputation: 10655
var data = {
"addresses": [
{
"address_type": "BUSINESS",
"city": "Belleville",
"country_code": "CA",
"line1": "47 Shawmut Ave.",
"line2": "Suite 404",
"postal_code": "K8b 5W6",
"state_code": "ON"
}
],
"lists": [
{
"id": "1395617465"
}
],
"cell_phone": "555-555-5555",
"company_name": "System Optimzations",
"confirmed": false,
"email_addresses": [
{
"email_address": "[email protected]"
}
],
"fax": "555-555-5555",
"first_name": "Ronald",
"home_phone": "555-555-5555",
"job_title": "Systems Analyst 3",
"last_name": "Martone",
"prefix_name": "Mr.",
"work_phone": "555-555-5555"
};
Now , convert object to JSON using JSON.stringify
and add Content-Type
header.
HTTP.post('https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=<api-key>',{
headers:{
'Authorization': 'Bearer <api-key>',
'Content-Type': 'application/json'
},
data: JSON.stringify(data),
function (error, response) {
if ( error ) {
console.log( error );
} else {
console.log(response);
}
});
Upvotes: 2