Ramya
Ramya

Reputation: 3

Getting exception org.springframework.web.client.HttpClientErrorException: 400 Bad Request when using restTemplate.postForEntity

public String getOrderdata(final String poNumber) throws Exception {
        String apiResponse = null;
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String url = ApiHost + ApiPath;
            JSONObject requestJson = new JSONObject();
            requestJson.put("orderNumber", poNumber);
            HttpEntity<String> request = new HttpEntity<>(requestJson.toString(), getHttpHeaders());
            log.info("request is " + request);
            log.info("Api url={}", url);
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
            log.info("Inside ServiceImpl, response is {}", response);
            if (response != null && response.hasBody() && response.getStatusCode() == HttpStatus.OK) {
                apiResponse = objectMapper.readValue(response.getBody(), String.class);
            } else if (response != null && response.getStatusCode() == HttpStatus.BAD_REQUEST) {
                log.info("event=getOrderdata: Received bad request exception from Api and Response is {} : ", response);
            } else {
                log.error("event=getOrderdata: Response is null= {} : ", response);
                throw new RuntimeException("Error While fetching details from api");
            }
        } catch (HttpClientErrorException e) {
            log.info("event=getOrderdata: HttpClientErrorException error=", e);
        } catch (Exception exe) {
            log.error("event=getOrderdata: error=", exe);
            //throw exe;
        }
        return apiResponse;
    }

    public HttpHeaders getHttpHeaders() {
        HttpHeaders headers = new HttpHeaders();
        try {
            val token = Client.getAccessToken(roleArn, scope);
            headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token);
            headers.setContentType(MediaType.APPLICATION_JSON);
            return headers;
        } catch (Exception ex) {
            throw new RuntimeException("Error trying to generate token", ex);
        }
    }

I am using springboot restTemplate.postForEntity(). I am able to hit through postman and trying to write java code for the same. I am hitting api using post call. For this endpoint token and below json body is required. I am able to generate token and below request is formed through above java code but at line number restTemplate.postForEntity() I am getting HttpClientErrorException: 400 Bad request. I am logging response string but It's not printing because of the above exception. Please help me.

JsonBody: { "orderNumber": "PO0012345" }

Request formed is:

 request is <{"orderNumber":"PO0012345"},{Authorization=[Bearer eyExampleToken], Content-Type=[application/json]}>

Please find the attachment which has postman request body. Click on this link to open it enter image description here

Upvotes: 0

Views: 876

Answers (1)

Anton Belev
Anton Belev

Reputation: 13503

Put a breakpoint on this line:

HttpEntity<String> request = new HttpEntity<>(requestJson.toString(), getHttpHeaders());
        

Inspect the value requestJson.toString(). Is it the same as the one you are successfully sending over Postman? Compare them. I think it will be hard for anyone to make a guess why the server responds with 400 otherwise. You can also compare the headers Postman is setting with yours in the Java code.

Upvotes: 2

Related Questions