简体   繁体   中英

HTTPBuilder and formUrlEncoded

First, sorry i am a newbie in groovy

I'm trying to make a request to google with HTTPBuilder ( https://github.com/jgritman/httpbuilder )

https://developers.google.com/identity/protocols/OAuth2ForDevices#obtainingacode

Here is my code

def code = new HTTPBuilder(oauthUrl).request(POST, URLENC) { req ->
    body = [client_id: clientId, scope: 'https://docs.google.com/feeds']

    headers.'Accept' = 'application/json'
    headers.'Content-Type' = 'application/x-www-form-urlencoded'
    response.success = { resp, reader ->
        println resp.statusLine
        println reader
        println reader.text
    }
    response.failure = { resp, reader ->
        println resp.statusLine
        println reader.text
    }
}

When i run this code, i get an output for println reader moreover resp.statusLine show me 200 Ok, but I get a null for println reader.text

I don't understand why this happen ...

The same request with curl send me the right response

{
  "device_code" : "XXXXX",
  "user_code" : "XXXX",
  "verification_url" : "https://www.google.com/device",
  "expires_in" : 1800,
  "interval" : 5
}

Edit

Here is the ouput

  • HTTP/1.1 200 OK
  • [{ "device_code" : "XXXk", "user_code" : "XXX", "verification_url" : " https://www.google.com/device ", "expires_in" : 1800, "interval" : 5 }:null]
  • null

Edit 2

I can access to the response with the following code (I'm not sure it is a clean code)

response.success = { resp, reader ->
    def stream
    reader.each { key, value ->
        stream = key
    }
    stream
}

As far as I know reader is an already parsed Map - you don't get an instance of stream, but the already parsed response. Since there's no text key you get null. Try:

println reader.device_code

it should work well.

You can also check what is reader with:

println reader.getClass()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM