0

I using java Unirest to call my api. but now I need to post an object but with x-www-form-urlencoded, I using this code:

public static void main(String[] args) {
        Unirest.config().setObjectMapper(new JacksonObjectMapper());
        System.out.println(Unirest
                .post("http://192.168.2.157:8082/auth/realms/collatum/protocol/openid-connect/token")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .body(RequestToken.builder()
                        .username("user")
                        .password("1234")
                        .grant_type("password")
                        .client_id("front")
                        .build()).asString().getBody()


        );
    }

and I get this error:

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

When I analise the request the header are ok:(but looks like the object are in json and no x-www-form-urlencoded

POST /auth/realms/collatum/protocol/openid-connect/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
user-agent: unirest-java/3.1.00
accept-encoding: gzip
Content-Length: 87
Host: 192.168.2.157:8082
Connection: Keep-Alive

{"grant_type":"password","client_id":"front","username":"user","password":"1234"}HTTP/1.1 400 Bad Request
Cache-Control: no-store
X-XSS-Protection: 1; mode=block
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
Referrer-Policy: no-referrer
Date: Thu, 20 Jan 2022 13:56:53 GMT
Connection: keep-alive
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Content-Type: application/json
Content-Length: 84

{"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
Fabio Ebner
  • 2,613
  • 16
  • 50
  • 77

1 Answers1

1

It looks like your body is in the wrong form for application/x-www-form-urlencoded.

It shouldn't be a json object and instead should be something like grant_type=password&client_id=front&username=user&password=1234

An example can be found here

Siddhartha Mukherjee
  • 2,703
  • 2
  • 24
  • 29
IAN
  • 11
  • 1