Home > Enterprise >  How to sent Http POST request with application/x-www-form-urlencoded
How to sent Http POST request with application/x-www-form-urlencoded

Time:11-17

I'm trying to generate a token from an external website, the post request must be application/x-www-form-urlencoded but I'm getting errors. I assume that I'm not making the right call for the content type application/x-www-form-urlencoded but I can't figuer out how!! PS: I'm using springboot with java 8

here is the code:

public String getNewAccesToken() {

        //Initilazing variabels
        try {

            JsonObject properties = new JsonObject();

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            headers.set("Cookie", cookie);
            properties.addProperty("client_id", clientId);
            properties.addProperty("client_secret", clientSecret);
            properties.addProperty("grant_type", grantType);
            
            HttpEntity<String> requestEntity = new HttpEntity<>(properties.toString(), headers);
            log.debug(requestEntity);
            ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity,
                    String.class);
            

            log.debug("---------------------------------");
            log.debug(response);
            if (Util.isJSONValid(response.getBody())) {
                JsonParser parser = new JsonParser();
                JsonObject jsonString = (JsonObject) parser.parse(response.getBody());
                return jsonString.get("accessToken").getAsString();
            } else {

                error.setCode(ConstantGateway.JSON_ERROR_CODE);
                error.setMessage(ConstantGateway.JSON_ERROR_STATUS);
                return error.toString();
            }
        } catch (HttpStatusCodeException e) {
            error.setCode(String.valueOf(e.getStatusCode().value()));
            error.setMessage(e.getResponseBodyAsString());
            return error.toString();
        } catch (Exception e) {
            // TODO: handle exception
            error.setCode(ConstantGateway.IL_INTERNAL_ERROR_CODE);
            error.setMessage(e.getMessage());
            return error.toString();
        }

    }

and here is what I get when calling this function:

org.zwz.vas.internal.api.model.ErrorModel@16dd359c

However when I make the call from Postman I get the right response which is :

{
    "access_token": "RdWt3DNIfxmihnubGX0Fgfcb0KNHLZV79OfN9Y6Ky6Z3fxAfF_Pm7uP0jnFrG1fHplyBMZ74BIKleQ8jmswdGy4e87NV-uZsMzgS1nQAONc2nBxgU1_jkMBhL4vvIniJNd99oYNzGeanCYYki0yorrrlLrOGTncusv1BgFFHU_CBGuUtGmZYLfJAJW4XcZLhXMC9xpT2aWAvgRXZW69pOhfU1Fgs7aVwou85UVI2b4j1GfX0pCtJtluiTgXsuWqdck7_at1dqfopHpjWAywYrweStMXGm8T59nyQi_oXWmo",
    "token_type": "bearer",
    "expires_in": 1199
}

THANK YOU IN ADVANCE

CodePudding user response:

I find the mistake I did, I was sending a wrong request format which is not compatibale with application/x-www-form-urlencoded type. The right code is below:


            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            headers.set("Cookie", cookie);
            
            MultiValueMap<String, String> properties= new LinkedMultiValueMap<String, String>();
            
            properties.add("client_id", clientId);
            properties.add("client_secret", clientSecret);
            properties.add("grant_type", grantType);
    
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(properties, headers);

            ResponseEntity<String> response = restTemplate.postForEntity( requestUrl, request , String.class );
            
  • Related