0

I am writing a code in grails which is making a post call using httpbuilder. everything works fine if I hardcode the values for body but when I pass the same using variables, then it keep giving me -

Unprocessable Entity. Stacktrace follows:
Message: Unprocessable Entity
   Line | Method
->> 636 | defaultFailureHandler in groovyx.net.http.HTTPBuilder
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   492 | doRequest             in     ''
|   427 | doRequest . . . . . . in     ''
|   376 | request               in     ''

The code is like below -

    def input = "s3://in_folder/inputFile.mp3"
    def outFile = "outFile.m3u8"
    def http = new HTTPBuilder("https://app.gridserver.com")
    http.request( groovyx.net.http.Method.POST, groovyx.net.http.ContentType.JSON ) { req ->
        uri.path = 'api/v2'
        headers.Accept = 'application/json'
        headers.'Key' = "ssdflkjdf8338fdjsd"
        body = ["input": input,  
            "output":[  "filename": outFile,
                        "format": "mp3",
                ] ]

        response.success = { resp, reader ->
            println "Got response: ${resp}"
        }
    }
}

If I replace the variables "input and outFile" here with the actual value then it works fine.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
saurabh
  • 2,389
  • 3
  • 22
  • 37
  • 1
    If you try using `"$input"` and `"$outFile"`? – dmahapatro Jun 14 '13 at 15:28
  • 1
    "unprocessable entity" is an HTTP 422 error code returned by the server. Can you [turn on wire logging](http://groovy.codehaus.org/modules/http-builder/doc/#Logging_and_Debugging) and verify exactly what is being sent in both cases? – Ian Roberts Jun 14 '13 at 15:36
  • @dmahapatro I have tried that as well. putting "${input}" but it still gives same error. – saurabh Jun 14 '13 at 15:44
  • @IanRoberts where do I change this in Grails? does it go anywhere in config.groovy? – saurabh Jun 14 '13 at 15:54
  • `"format": "mp3",` why do you have `,` in the last element? – dmahapatro Jun 14 '13 at 15:56
  • @dmahapatro that is the format the service is expecting. that is the json format for the service. the main issue I am facing here is when I hardcode the string value for input and outFile variable then it works fine. so for some reason it is not able to translate these variable tere. – saurabh Jun 14 '13 at 15:58
  • Yes, add `org.apache.http.headers` and `org.apache.http.wire` in `DEBUG` logging level in config. Body is good, I was skeptical until I validated it. – dmahapatro Jun 14 '13 at 16:20
  • Yes, you'd add `debug 'org.apache.http.wire'` inside the `log4j` closure in `Config.groovy`. – Ian Roberts Jun 14 '13 at 16:20
  • ok with the logging enabled, I can see that variable values is not passed inside the closure. Now how do I pass the value inside. I think it has become more of a groovy question now – saurabh Jun 14 '13 at 16:27
  • Post the log portion where you do not see the values also which version of `HTTPBuilder` are you using. – dmahapatro Jun 14 '13 at 16:33
  • actually now I notice the value is there but it is converting the variable into a json equivalent. so the value looks like "{"input":{"strings":["s3://audio_in/",".mp3"],"valueCount":1,"values":[1371175948504]} while it should look like "{"input": "s3://audio_in/1371175948504.mp3"] ... my expression looks simply like "{"input": "s3://audio_in/${fileName}.mp3"] – saurabh Jun 14 '13 at 16:39
  • 1
    ok, I seems to have solved it finally. I needed to do the .toString() in the expression so that it takes it like a literal and dont convert it into a json. so finally I wrote it like "s3://audio_in/${fileName}.mp3".toString() and it works fine. – saurabh Jun 14 '13 at 16:41
  • 1
    Alternatively if you declare it as `String input = "..."` instead of `def input = "..."` then groovy will convert the gstring to a string automatically. – Ian Roberts Jun 14 '13 at 17:49
  • @IanRoberts I will try that as well but I think I had it earlier and it was not working then. It might be another error at that moment. I will certainly check it and update here. – saurabh Jun 14 '13 at 18:57
  • Thanks for your help guys. I finally managed to solve it with your help. – saurabh Jun 17 '13 at 14:52

1 Answers1

0

ok, I seems to have solved it finally. I needed to do the .toString() in the expression so that it takes it like a literal and dont convert it into a json. so finally I wrote it like "s3://audio_in/${fileName}.mp3".toString() and it works fine.

saurabh
  • 2,389
  • 3
  • 22
  • 37