69

I have keycloak standalone running on my local machine.

I created new realm called 'spring-test', then new client called 'login-app'

According to the rest documentation:

POST: http://localhost:8080/auth/realms/spring-test/protocol/openid-connect/token

{
    "client_id": "login-app",
    "username": "user123",
    "password": "pass123",
    "grant_type": "password"
}

should give me the jwt token but I get bad request with response

{
    "error": "invalid_request",
    "error_description": "Missing form parameter: grant_type"
}

I am assuming that something is missing in my configuration.

EDIT: I was using json body but it should be application/x-www-form-urlencoded: the following body works:

token_type_hint:access_token&token:{token}&client_id:{client_id}&client_secret:{client_secret}

10 Answers 10

83

You should send your data in a POST request with Content-Type header value set to application/x-www-form-urlencoded, not json.

5
  • 7
    I'm still having the same issue, even if i set the content type to be urlencoded: curl -d '{"grant_type": "password", "username": "user", "password": "pass", "client_id": "login-app"}' -H "Content-Type: application/x-www-form-urlencoded" -X POST "localhost:8082/auth/realms/ina-dev/protocol/openid-connect/…" {"error":"invalid_request","error_description":"Missing form parameter: grant_type"} Feb 7, 2019 at 10:18
  • 2
    because you send json data, you should use -d "param1=value1&param2=value2" syntaxis
    – ipave
    Feb 8, 2019 at 16:31
  • 3
    This is not working. How do you set that -d stuff in postman, @ipave?
    – Avión
    Apr 24, 2019 at 14:25
  • 3
    @Avión here is a link - learning.getpostman.com/docs/postman/sending_api_requests/…
    – ipave
    Apr 25, 2019 at 13:42
  • 1
    Thanks, I was sending "multipart/form-data; boundary=--------------------------616846104444017186133807"
    – masterxilo
    Jun 6, 2019 at 13:42
22

With Curl

curl -X POST \
http://localhost:8080/auth/realms/api-gateway/protocol/openid-connect/token \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 73' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: JSESSIONID=F8CD240FF046572F864DC37148E51128.a139df207ece;   JSESSIONID=65D31B82F8C5FCAA5B1577DA03B4647C' \
-H 'Host: localhost:8080' \
-H 'Postman-Token: debc4f90-f555-4769-b392-c1130726a027,d5087d9f-9253-48bd-bb71-fda1d4558e4d' \
-H 'User-Agent: PostmanRuntime/7.15.2' \
-H 'cache-control: no-cache' \
-d 'grant_type=password&client_id=api-gateway&username=admin&password=temp123'

By Postman (Select x-www-form-urlencoded option for parameters)

enter image description here

1
  • 4
    don't forget to put client_secret
    – aswzen
    Jul 7, 2020 at 17:14
22

For those who landed here from a search looking for JavaScript solution.

Here is an example when exchanging code for access_token with keycloak authority using axios.

Sending the request:


const params = new URLSearchParams({

    grant_type: 'authorization_code',
    client_id: 'client-id-here',
    code: 'code-from-previous-redirect',
    redirect_uri: location.protocol + '//' + location.host

});

axios({

    method: 'post',
    url: 'https://my-keycloak.authority/token',
    data: params.toString(),
    config: {
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }

}).then(response => {

    console.log(response.data);

}).catch(error => {

    console.error(error);

});

You are required to send a POST request with the parameters as a URL encoded string in the request body.

FormData object does not work.

4
  • There is an easy way to get token without using query string. const config = { headers: {'Content-Type': 'application/x-www-form-urlencoded'} }; const formParameter = client_id=KEYCLOAK_CLIENT_ID&client_secret=KEYCLOAK_CLIENT_SECRET&grant_type=KEYCLOAK_API_GRANT_TYPE; axios.post(REQUEST_URL,formParameter,config); Jan 15, 2021 at 9:45
  • Pavan J posted an Answer saying "Thanks @marc for the solution and it works! I have been looking for this solution in quite a number of forums. PS: There's a typo in grant_type: 'authorization_code', (closing quote missing)"
    – Scratte
    Apr 19, 2021 at 17:16
  • querystring is depricated now github.com/nodejs/node/blob/v17.0.0/lib/querystring.js
    – Sudarshan
    Mar 16, 2022 at 14:46
  • Since querystring is depricated as @sudarshan pointed out, we can use URLSearchParams instead. In this code snippet we can substitute new URLSearchParams(params) for queryString.stringify(params). URLSearchParams should be supported in newer versions of node and the browser. If your version of node doesn't have support for it we can use a polyfill Apr 7, 2022 at 17:16
16

For those having problems with curl the curl command is as follows

curl -d "client_secret=<client-secret>" -d "client_id=<client-id>" -d "username=<username>" -d "password=<password>" -d "grant_type=password" "http://localhost:8080/auth/realms/<realm-name>/protocol/openid-connect/token"

The curl command works without the Content-Type header.

0

Here's a sample CURL command

curl -X POST \
  http://localhost:8080/auth/realms/your_realm_name/protocol/openid-connect/token \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 69' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Cookie: KC_RESTART=' \
  -H 'Host: localhost:8080' \
  -H 'Postman-Token: 88f38aa0-8659-4b37-a2aa-d6b92177bdc2,29c4e7db-51f4-48d1-b6d5-daab06b68ab4' \
  -H 'User-Agent: PostmanRuntime/7.20.1' \
  -H 'cache-control: no-cache' \
  -d 'client_id=my-app&username=admin&password=admin123&grant_type=password'
0

I had similar issue in SOAPUI testing. We should not POST a json. This got resolved when I cleared 'Media Type' and checked 'PostQueryString' checkbox. 'Media Type box' will set to 'www-form-urlencoded' by itself. Add attributes at Top by hitting plus sign.

enter image description here

0
0

ERROR TYPE: { "error": "invalid_request", "error_description": "Missing form parameter: grant_type" }

Hi all, if you are experiencing an error in ARC "grant_type" when making a POST request to test keycloak you can do the following modifications in the parameter: set HEADER to "content_type" and set the value to "application/x-www-form-urlencoded"

1
0

Expanding the solution provided Accepted Answer

The below code is directly in application/x-www-form-urlencoded format

`grant_type=${encodeURIComponent('authorization_code')}&code=${encodeURIComponent(this.authorizationCode)}&redirect_uri=${encodeURIComponent(redirectUri)}&client_id=${encodeURIComponent(clientId)}&code_verifier=${encodeURIComponent(codeVerifier)}`
0
0

If you still have this problem - double check if your client and user belong to the realm in post request.

0

after changing Content-Type from json to application/x-www-form-urlencoded

you must convert json body as follows

token_type_hint:access_token&token:{token}&client_id:{client_id}&client_secret:{client_secret}

see this example:

POST http://localhost:8080/realms/master/protocol/openid-connect/token
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Connection: keep-alive

grant_type=password&client_id=springDemo&username=user01&password=user01&client_secret=greJdi4wZCxHsu4AF7HRlLKRwvX8AQBU

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.