0

I'm making a apex rest class and trying to test it with POSTMAN. I already manage to do the authorization part succesfully, but when i make the call i receive as a response this error:

enter image description here

After viewing the logs i figured out that this error is because the body of the request is never sended to the apex class. Any idea why is this occurs?

enter image description here

As you can see in the image, the class is receiving no body in the request (blob[0]) so it cant deserialize into the wrapper because there is nothing there.

i also leave this image from the JSON body in postman

enter image description here enter image description here

EDIT: I forgot to add the code with the data parsing, so i going to paste it here -

    @RestResource(urlMapping='/RectorUpdaterRest/')
    global with sharing class RectorUpdaterREST {
    
        @httpPut
        global static String updateRecords () {
            RestRequest request = RestContext.request;
    
            String peticion = request.params.get('peticion');
    
            responseWrapper result = new responseWrapper();
            switch on peticion {
                when 'findCliente'{
                    system.debug(request.requestBody);
                    FindClientWrapper requestedValues = (FindClientWrapper)JSON.deserialize(request.requestBody.toString(), FindClientWrapper.class);
                    result = RectorUpdaterRESTHelper.proccessItemsReceived(requestedValues);
                }
                when 'getProductosCliente'{
                    GetProductosClienteWrapper requestedValues = (GetProductosClienteWrapper)JSON.deserialize(request.requestBody.toString(), GetProductosClienteWrapper.class);
                    system.debug(requestedValues);
                    result = RectorUpdaterRESTHelper.upsertProductosClientes(requestedValues);
                }
                when else {
                    result.message = 'pieza no updateada';
                    result.code = '500';
                }
            }
    
            return JSON.serialize(result);
        }

And here is the wrapper:

    public with sharing class FindClientWrapper {

    public List<ClientWrapper> clientes {get; set;}
    
    public FindClientWrapper() {
    }

    public class ClientWrapper {
    
        public String id {get; set;}
        public String nombre {get; set;}
        public String apellido {get; set;}
        public String sexo {get; set;}
        public Date fechaNacimiento {get; set;}
        public String tipoDocumento {get; set;}
        public String numeroDocumento {get; set;}
        public String cuit {get; set;}
        public String nacionalidad {get; set;}
        public String razonSocial {get; set;}
        public String estadoCivil {get; set;}
        public String categoriaIVA {get; set;}
        public String categoriaIB {get; set;}
        public Boolean cobrador {get; set;}
        public Boolean percepcionIVA {get;set;}
        public Boolean retencionIVA {get; set;}
        public Boolean percepcionIB {get; set;}
        public Boolean retencionIB {get; set;}
        public String IdCliente {get; set;}
        public String idProductor {get; set;}
        public String idClienteBH {get; set;}
        public Datetime fechaUltimaBH {get; set;}
        public String score {get; set;}
        public List<DomicilioWrapper> domicilios {get; set;}
        public List<TelefonosWrapper> telefonos {get; set;}
        public List<EmailsWrapper> emails {get; set;}
        public List<ReintegrosWrapper> reintegros {get; set;}
        public List<BonificacionesWrapper> bonificaciones {get; set;}

        public ClientWrapper(){

        }
    }


    public class DomicilioWrapper {
        public String idPersona {get; set;}
        public String numeroConsecutivo {get; set;}
        public String idTipoDomicilio {get; set;}
        public String idCodigoPostal {get; set;}
        public String calle {get; set;}
        public String numero {get; set;}
        public String piso {get; set;}
        public String departamento {get; set;}
        public Datetime fechaUltimaBH {get; set;}

        public DomicilioWrapper() {

        }
    }

    public class TelefonosWrapper {
        public String idPersona {get; set;}
        public String numeroConsecutivo {get; set;}
        public String codigoContacto {get; set;}
        public String descripcion {get; set;}

        public TelefonosWrapper(){

        }
    }

    public class EmailsWrapper {
        public String idPersona {get; set;}
        public String numeroConsecutivo {get; set;}
        public String codigoContacto {get; set;}
        public String descripcion {get; set;}
        public String estado {get; set;}

        public EmailsWrapper (){

        }
    }

    public class ReintegrosWrapper {
        public String remesa {get; set;}
        public String cuenta {get; set;}
        public String ramo {get; set;}
        public String poliza {get; set;}
        public String certificado {get; set;}
        public String impADevolver {get; set;}
        public String estado {get; set;}

        public ReintegrosWrapper() {

        }
    }

    public class BonificacionesWrapper {
        public String nroGestion {get; set;}
        public String nroNota {get; set;}
        public String bonificacionDes {get; set;}
        public Date fechaBonificacion {get; set;}
        public Date fechaAplicacion {get; set;}
        public String estadoGestion {get; set;}
        public String estadoNota {get; set;}
        public String cuotas {get; set;}
        public String porCuota {get; set;}
        public String importe {get; set;}
        public String importePremioReferencia {get; set;}
        public String importeBonificacionReferencia {get; set;}
        public String importePlicado {get; set;}

        public BonificacionesWrapper() {

        }
    }
}
3
  • 1
    We would need to see how you're parsing your data (show your Apex). Without this, we can't help you. Include the HttpPost part as well (method name, return type, and parameters). – sfdcfox Aug 3 '20 at 20:47
  • 1
    Please include as much information as possible using text (format code blocks by wrapping with ```) rather than screenshots. – David Reed Aug 3 '20 at 20:52
  • done! i already put the code in the post, thanks for the help! – Lucas Campos Aug 3 '20 at 21:02
0

Since you know the input is empty. Its better to add NULL/Empty check and return the response accordingly.

E.g. you can have code like this -

 @httpPut
    global static String updateRecords () {
        RestRequest request = RestContext.request;

        String peticion = request.params.get('peticion');

        responseWrapper result = new responseWrapper();
        if(! (request.requestBody != NULL && request.requestBody.toString().trim() == '' )
        {
                result.message = 'Bad Request. Please check your request body.'; //you can have custom message here
                result.code = '400';
                return JSON.serialize(result);
        } 


        switch on peticion {
            when 'findCliente'{
                system.debug(request.requestBody);
                FindClientWrapper requestedValues = (FindClientWrapper)JSON.deserialize(request.requestBody.toString(), FindClientWrapper.class);
                result = RectorUpdaterRESTHelper.proccessItemsReceived(requestedValues);
            }
            when 'getProductosCliente'{
                GetProductosClienteWrapper requestedValues = (GetProductosClienteWrapper)JSON.deserialize(request.requestBody.toString(), GetProductosClienteWrapper.class);
                system.debug(requestedValues);
                result = RectorUpdaterRESTHelper.upsertProductosClientes(requestedValues);
            }
            when else {
                result.message = 'pieza no updateada';
                result.code = '500';
            }
        }

        return JSON.serialize(result);
    }
2
  • i already try that but still receiving the same error. Its never enters to the first if and it goes directly to the switch case. It could be a settings problem in postman? – Lucas Campos Aug 4 '20 at 19:29
  • May you can tweak the condition. It has to have first check of null or empty part, then only it should go to the switch block. If you notice i added return in IF block, which return back, so that it won't go further. – Ysr Shk Aug 5 '20 at 2:50

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.