1
votes

postman request body

As seen in the image above, I am sending grant_type=client_credentials as post body. The equivalent curl command is

curl --location --request POST 'https://localhost:8080/token'
--header 'Authorization: Basic **************************************'
--header 'Content-Type: application/x-www-form-urlencoded'
--data-urlencode 'grant_type=client_credentials'

I am able to send grant_type=client_credentials as query parameter and get a successful response. But we are asked not to use as query param because of company policy and asked to send it in body.

The code which I came up with is

public Message<Map<String, String>> getTokenGenRequest() {
        setHeaderMapper(new String[] { TransferConstants.AUTHORIZATION, TransferConstants.CONTENT_TYPE });
        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("grant_type", "client_credentials");
        return MessageBuilder.withPayload(requestMap)
                .setHeader(TransferConstants.AUTHORIZATION, getAuthCredentials())
                .setHeader(TransferConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED).build();
    }


private void setHeaderMapper(String[] headerNames) {
        headerMapper.setOutboundHeaderNames(headerNames);

    }

However the request goes out Writing [{grant_type=[client_credentials]}] as "application/x-www-form-urlencoded" due to which I am not getting any response.Can somebody tell me what is wrong with the code?

1

1 Answers

0
votes

The {grant_type=[client_credentials]} is a correct x-www-form-urlencoded content type. See HTTP specification: https://url.spec.whatwg.org/#urlencoded-serializing.

You can try to build an RequestEntity upfront with the body as a string of that pair, plus add those header manually.

I mean your getTokenGenRequest() should return a Message<HttpEntity<?>>, but, of course, it would be better to consult with another side (REST service) to be sure what they really want from you. Probably you don't sent some extra header or there is some which they don't accept.