-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. POSTMAN Screen shot successfull

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());
    }
9
  • Well, is the remote service expecting multipart/form-data, application/x-www-form-urlencoded, or JSON payload? You're sending JSON with the wrong Content-Type right now.
    – David Reed
    Jul 25, 2018 at 13:27
  • @DavidReed The remote string is expecting the body in form data so i think it is Multipart/form-data correct me if i am wrong
    – Avesh
    Jul 25, 2018 at 13:32
  • I can't correct you because I don't know the requirements of the service you're calling (or even what it is). Your screenshot looks like you are sending form-data encoding from Postman. If that's indeed what the service requires, your Apex should do the same, rather than sending JSON with the incorrect Content-Type multipart/form-data header.
    – David Reed
    Jul 25, 2018 at 13:46
  • @DavidReed I have removed this header but still getting the same issue. request.setHeader('Content-Type', 'application/json');
    – Avesh
    Jul 25, 2018 at 13:49
  • 1
    I can't write your code for you. (That's also not what we do at Stack Exchange). Please see How to Ask to get a better sense of what SFSE needs from you and can provide.
    – David Reed
    Jul 25, 2018 at 14:03

1 Answer 1

2

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.