Grails RestBuilder simple POST example

后端 未结 3 1735
谎友^
谎友^ 2020-12-29 07:17

I\'m trying to do an OAuth2 user-credentials post to an OAuth2 service using the Grails RestBuilder plugin.

If I try to specify the post body as a map, I get an erro

3条回答
  •  旧巷少年郎
    2020-12-29 08:02

    I solved the problem by using a MultiValue map for the body.

    RestBuilder rest = new RestBuilder()
    MultiValueMap form = new LinkedMultiValueMap()
    form.add("grant_type", "password")
    form.add("username", username)
    form.add("password", password)
    def resp = rest.post("http://${hostname}/oauth/token") {
        auth(clientId, clientSecret)
        accept("application/json")
        contentType("application/x-www-form-urlencoded")
        body(form)
    }
    def json = resp.json
    

提交回复
热议问题