1

I am trying to send SMS via Marketing Cloud from SF Apex.

My postman is working fine and it is shown below

Postman Payload

{
    "mobileNumbers": [
    "PHONENUMBERHERE"
    ],
    "Subscribe": true,
    "Resubscribe": true,
    "keyword": "EMPCOM1",
    "Override": true,
    "messageText": "This is a test"

   
}

enter image description here

I am trying to do the same thing in Anon Apex.

Heres my code

JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('mobileNumbers');
gen.writeStartArray();
gen.writeString('PHONENUMBERHERE');
gen.writeEndArray();
gen.writeBooleanField('Subscribe', true);
gen.writeBooleanField('Resubscribe', true);
gen.writeStringField('keyword', 'EMPCOM1');
gen.writeBooleanField('Override', true);
gen.writeStringField('messageText', 'This is a test');
gen.writeEndObject();
String jsonData = gen.getAsString();
System.debug(jsonData);     


String accessToken = 'ACCESS TOKEN HERE';

String authString = 'Bearer '+accessToken;

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://MCIDHERE.rest.marketingcloudapis.com/sms/v1/messageContact/XXX/send');
request.setMethod('POST');
request.setHeader('Authorization', authString);
request.setBody(jsonData);

System.debug(jsonData);     
    
HttpResponse response = new HttpResponse();
response = http.send(request);

System.debug(response.getBody());

When executed in dev console, I am getting the following output.

As can be seen, the payload which i send from Postman is exactly the same as the one I derive using the JSONGenerator class.

I am unable to understand as to why it is complaining that the subscriber/phone number data is not being sent.

Can someone let me know why this is the case ?

enter image description here

1 Answer 1

1

You forgot to set the header Content-Type: application/json.

Add it to your code then it should be fine.

JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeFieldName('mobileNumbers');
gen.writeStartArray();
gen.writeString('PHONENUMBERHERE');
gen.writeEndArray();
gen.writeBooleanField('Subscribe', true);
gen.writeBooleanField('Resubscribe', true);
gen.writeStringField('keyword', 'EMPCOM1');
gen.writeBooleanField('Override', true);
gen.writeStringField('messageText', 'This is a test');
gen.writeEndObject();
String jsonData = gen.getAsString();
System.debug(jsonData);     


String accessToken = 'ACCESS TOKEN HERE';

String authString = 'Bearer '+accessToken;

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://MCIDHERE.rest.marketingcloudapis.com/sms/v1/messageContact/XXX/send');
request.setMethod('POST');
request.setHeader('Authorization', authString);
request.setHeader('Content-Type', 'application/json');
request.setBody(jsonData);

System.debug(jsonData);     
    
HttpResponse response = new HttpResponse();
response = http.send(request);

System.debug(response.getBody());

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .