Error 403 Forbidden while consuming CEP webservice

Asked

Viewed 717 times

2

I’m trying to consume a webservice of ceps but always returns me 403 and it is public.

Testing the Working Webservice :

http://apps.widenet.com.br/busca-cep/api/cep.json?code=01001000

My Controller

@Controller
@RequestMapping("/busca-cep")
public class BuscaCEPController {

    private static final Logger LOG = LoggerFactory.getLogger(BuscaCEPController.class);

    @Autowired
    private LogService logService;

    @Autowired
    private BuscaCEPService buscaCEPService;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<CEPResult> buscaCEP(String code) {
        try {
            logService.info(LOG, "action=buscaCEPIniciado, cep={}", code);
            CEPResult result = buscaCEPService.buscarCEP(code); 
            logService.info(LOG, "action=buscaCEPConcluido, cep={}", code);
            return new ResponseEntity<CEPResult>(result, HttpStatus.OK);
        } catch (Exception e) {
            logService.error(LOG, "action=buscaCEPErro, cep={}, e={}", code, e.getMessage());
            return new ResponseEntity<CEPResult>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}

My Consumer Service

  @Service
    public class BuscaCEPService {

        public CEPResult buscarCEP(String code) {
            RestTemplate restTemplate = new RestTemplate();
            CEPResult result = restTemplate.getForObject("http://apps.widenet.com.br/busca-cep/api/cep.json?code=" + code, CEPResult.class);
            return result;
        }

    }

1 answer

3


I tested calls to this provided URL example:

http://apps.widenet.com.br/busca-cep/api/cep.json?code=01001000

What I realized is causing the 403 is that the server needs the header User-Agent is present in the request, regardless of the header content, and may be empty.

So what we need to do is put this header so that the request made goes with it.

With the getForObject of RestTemplate there is no way to inform headers, an alternative is to run the exchange passing a HttpEntity with the necessary headers.

A demonstrated example of this approach would be thus:

private static final String URL = "http://apps.widenet.com.br/busca-cep/api/cep.json?code={code}";

public String findCEPByCode(final String code) {
    final RestTemplate template = new RestTemplate();

    final HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.USER_AGENT, "");

    final HttpEntity<?> entity = new HttpEntity<>(headers);

    final ResponseEntity<String> result = template.exchange(URL, HttpMethod.GET, entity, String.class, code);

    return result.getBody();
}

public static void main(final String[] args) {
    System.out.println(new CEPService().findCEPByCode("01001000"));
}

Which will generate this JSON (in Unicode why I didn’t force anything to UTF-8):

{
   "status":1,
   "code":"01001-000",
   "state":"SP",
   "city":"S\u00e3o Paulo",
   "district":"S\u00e9",
   "address":"Pra\u00e7a da S\u00e9 - lado \u00edmpar"
}

To return your CEPResponse, just change the type, getting like this:

public CEPResponse findCEPByCode(final String code) {
    final RestTemplate template = new RestTemplate();

    // dependendo das configurações do contexto do spring não precisará disto
    template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

    final HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.USER_AGENT, "");

    final HttpEntity<?> entity = new HttpEntity<>(headers);

    final ResponseEntity<CEPResponse> result = template.exchange(URL, HttpMethod.GET, entity, CEPResponse.class, code);

    return result.getBody();
}
  • Thank you , worked perfectly !

  • just out of curiosity as you discovered that you would need to pass the user-agent header?

  • @Josevieiraneto as is GET, so I tested by the browser first. As the browser always puts this header (and others), hence I inferred that it was absence of some header (must be WS server configuration), so I tested and bingo :)

  • Got it, thanks for the return

Browser other questions tagged

You are not signed in. Login or sign up in order to post.