1

I am posting the data to endpoint url through http callouts from developer console and getting response "200 ok" and in body of response i am getting an error message of

"{"IsSuccess":false,"Message":"10 digit card number cannot be empty"}"

even though i have inserted 10 digit card number but when i am trying to insert the same data using POSTMAN then data is posted and also getting created in the database but when i am posting through APEX then it is not getting inserted bcoz of the above error.

Also i am attaching the screenshot of POSTMAN from where i am inserting the data in form data format. Can anyone please help me on my query. Formdata using postman

Http p=New Http();    
Httprequest req=new Httprequest();
    Info_Saloon__c MyToken = Info_Saloon__c.getInstance('IFS Links');
req.setendpoint(MyToken.Info_Saloon_Token__c);
req.setmethod('POST');

string Body=MyToken.Token_Body__c;  
req.setbody(body);
req.setheader('Content-Type','application/x-www-form-urlencoded');
System.HttpResponse response1 = new System.Http().send(req);
system.debug(''+response1.getBody());
string AuthorizationToken;
System.JSONParser jp=JSON.createParser(response1.getBody());
while(jp.nextToken()!=null){
    if(jp.getText()=='access_token'){
        jp.nextToken();
        AuthorizationToken=jp.getText();
    }
}
Http http = new Http();

HttpRequest request = new HttpRequest();
request.setEndpoint(MyToken.Insert_Data__c);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Content-Type','multipart/form-data; boundary=---WebKitFormBoundary7MA4YWxkTrZu0gW');


request.setHeader('Authorization','Bearer '+AuthorizationToken);


request.setBody('[{"Full_Card_Number__c" :"1234567895","First_Name__c" :"Avesh","Last_Name__c :"Test","Contact_Email__c" :"test@test.com","Category__c" :"DEL","Conference_Type__c" :"STRAT","Conference_Selection__c" :"ST4A","Payment_Status_Interface__c" :"NPAID"}]');

HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
    System.debug('The status code returned was not expected: ' +
                 response.getStatusCode() + ' ' + response.getStatus());
    System.debug(response.getBody());
} else {
    System.debug(response.getBody());
}
1

Since I assume your postman call works while your Apex one doesn't, I suggest you change it slightly so that you can find the problem in your JSON. What I would do is insert the headers as such and send a raw JSON via Postman, that way you can play around with the JSON and try to find the bug.

As of your current code, it's fine. and the response demonstrates that it's fine. The problem is with the JSON - the server isn't accepting it.

I would further recommend to double check it with the documentation. For example, I would dare guess that your Full_Card_Number__c should be an integer, while in the JSON you're sending it as a String. That could potentially be the error. But again, that's just a guess.

  • I have changed the format to raw instead of Form Data in Postman and tried to post then I am getting the same error which i am getting in Apex. – Avesh Jul 30 '18 at 10:00
  • @Avesh have you verified the types of the data? – Javier García Manzano Jul 30 '18 at 10:00
  • Yes i have changed Full card number to integer as well – Avesh Jul 30 '18 at 10:07
  • @Avesh I cannot help you more then. It could perfectly be a problem within the server application where you are making the callout to. No idea. – Javier García Manzano Jul 30 '18 at 12:12
  • I change the body to request.setBody('------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"First_Name__c\"\r\n\r\nAvesh\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Last_Name__c\"\r\n\r\nTest\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Contact_Email__c\"\r\n\r\navesh@test.com\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Full_Card_Number__c\"\r\n\r\n1234567896\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--'); – Avesh Aug 5 '18 at 7:15
0

Here is the working code

Http p=New Http();    
Httprequest req=new Httprequest();
    Info_Saloon__c MyToken = Info_Saloon__c.getInstance('IFS Links');
    req.setendpoint(MyToken.Info_Saloon_Token__c);
    req.setmethod('POST');

    string Body=MyToken.Token_Body__c;  
    req.setbody(body);
    req.setheader('Content-Type','application/x-www-form-urlencoded');
    System.HttpResponse response1 = new System.Http().send(req);
    system.debug(''+response1.getBody());
    string AuthorizationToken;
    System.JSONParser jp=JSON.createParser(response1.getBody());
    while(jp.nextToken()!=null){
        if(jp.getText()=='access_token'){
            jp.nextToken();
            AuthorizationToken=jp.getText();
        }
    }
Http http = new Http();

    HttpRequest request = new HttpRequest();
    request.setEndpoint(MyToken.Insert_Data__C);
    request.setMethod('POST');
    request.setHeader('Content-Type','application/json');
    request.setHeader('Content-Type','multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');
    request.setHeader('Authorization','Bearer '+ AuthorizationToken);


List<registration__C> Reg=[SELECT Salutation__c, First_Name__c, Last_Name__c ,Account_Name__c, Contact_Email__c, Category__c, Conference_Type__c, Conference_Selection__c, Payment_Status_Interface__c,  Full_Card_Number__c
FROM Registration__C limit 2];
for(integer i=0;i<Reg.size();i++)
{
    //request.setbody(body);
request.setbody('------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Full_Card_Number__c\"\r\n\r\n'+Reg[i].Full_Card_Number__c+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Salutation__c\"\r\n\r\n'+Reg[i].Salutation__c+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"First_Name__c\"\r\n\r\n'+Reg[i].First_Name__C+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Last_Name__c\"\r\n\r\n'+Reg[i].Last_Name__C+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Account_Name__c\"\r\n\r\n'+ Reg[i].Account_Name__C+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Contact_Email__c\"\r\n\r\n'+Reg[i].Contact_Email__C+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Category__c\"\r\n\r\n'+Reg[i].Category__C+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Conference_Type__c\"\r\n\r\n'+Reg[i].Conference_Type__C+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Conference_Selection__c\"\r\n\r\n'+Reg[i].Conference_Selection__C+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Payment_Status_Interface__c\"\r\n\r\n'+Reg[i].Payment_Status_Interface__C+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--');
    HttpResponse response = http.send(request);
    // Parse the JSON response
    if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
                     response.getStatusCode() + ' ' + response.getStatus());
        System.debug(response.getBody());
    } else {
        System.debug(response.getBody());
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.