1

I'm trying to send and SMS through serwersms and creation a Callout with JSON. This request works fine:

https://api2.serwersms.pl/messages/send_sms.json?username=LOGIN&password=PASS&phone=48500600700&text=TEXT&details=true

But my JSON doesn't. I'm getting {"error":{"code":1001,"type":"InvalidUser","message":"Nieprawid\u0142owy login lub has\u0142o"}}. And it's 100% correct username and password.

My JSON:

JSONGenerator gen = JSON.createGenerator(true);    
gen.writeStartObject();      
gen.writeStringField('username', 'LOGIN');
gen.writeStringField('password', 'PASS');
gen.writeFieldName('messages');
    gen.writeStartArray();
        for (integer i=0; i<2; i++){
            gen.writeStartObject();
                gen.writeStringField('phone', '48500600700');
                gen.writeStringField('text', 'TEXT');
            gen.writeEndObject();
        }
    gen.writeEndArray();
gen.writeBooleanField('details', true);
gen.writeEndObject();

String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);
String endpoint = 'https://api2.serwersms.pl/messages/send_sms.json'; //or maybe 'https://api2.serwersms.pl/' is right?
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setBody(jsonS);
Http http = new Http();
req.setTimeout(20000);
HTTPResponse response = http.send(req);
System.debug(response.getBody());

Documentation:

https://dev.serwersms.pl/en/https-api-v2/introduction and https://en.serwersms.pl/download/59 (14th page).

Help me please to fix it.

3
  • What does the error message mean, in English?
    – David Reed
    Commented Nov 7, 2019 at 17:11
  • @DavidReed According to Google, "Incorrect login or password"
    – sfdcfox
    Commented Nov 7, 2019 at 17:20
  • 1
    I don't think this API wants you to send JSON to the endpoint. Instead, I think it wants you to send form-data` instead. And then the response back will be JSON Commented Nov 7, 2019 at 17:22

1 Answer 1

2

To expand upon my comment above, I think it is looking for a POST of application/x-www-form-urlencoded data. So you would have to re-create this POSTMAN output in Apex:

POST /messages/send_sms.json HTTP/1.1
Host: api2.serwersms.pl
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
Postman-Token: 137ae9bb-5f63-4e4b-8637-fe83445be7a5

username=un&password=pw&phone=%2B1234567890&text=Hello+World

EDIT

Here is an Apex Example:

map<String,String> formData = new map<String,String>();

formData.put('username','un');
formData.put('password','un');
formData.put('phone','+1234567890');
formData.put('text','Hello World');

String[] bodyArry = new String[]{};

for(String s : formData.keyset())
{
    String currVal = EncodingUtil.urlEncode(formData.get(s),'UTF-8');

    bodyArry.add(s+'='+currVal);
}

Http h = new Http();

HttpRequest req = new HttpRequest();

req.setEndpoint('https://api2.serwersms.pl/messages/send_sms.json');
req.setMethod('POST');
req.setBody(String.join(bodyArry,'&'));
2
  • Never worked with Postman before. And still don't understand how to make it in Apex.
    – Apelsinova
    Commented Nov 7, 2019 at 21:00
  • 1
    @Apelsinova I have updated my answer Commented Nov 8, 2019 at 14:20

You must log in to answer this question.

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