http-builder-ng / gradle-http-plugin

Gradle plugin providing support for using HttpBuilder-NG to make HTTP requests as Gradle Tasks.
https://http-builder-ng.github.io/gradle-http-plugin/
Apache License 2.0
31 stars 8 forks source link

Request.body sending null #9

Closed arundotin closed 6 years ago

arundotin commented 6 years ago

I am making Http Post call using the plugin io.github.http-builder-ng.http-plugin. Below is my build.gradle

plugins {
     id "io.github.http-builder-ng.http-plugin" version "0.1.1"
}
task makeRESTCall (type: io.github.httpbuilderng.http.HttpTask) {
    onlyIf {
        !dependencyList.empty // this list is set by other task 
    }
    doFirst {
        println dependencyList // printing fine
    }
    config {
        request.uri = 'http://localhost:8080'
        request.contentType = 'application/json'
        request.accept = '*'
        request.uri.path = '/api/v1/validate'

    }
    post {
        request.body = dependencyList
        response.success {
            println "Success"
        }
    }
}

This is my dependencyList parameter that am sending in request.body

[

{
    "groupId":"org.hibernate",
    "artifactId":"hibernate-validator",
    "version":"5.3.6.Final"
}, 
{
    "groupId":"org.projectlombok",
    "artifactId":"lombok",
    "version":"1.16.22"
}, 
{
    "groupId":"io.springfox",
    "artifactId":"springfox-swagger-ui",
    "version":"2.8.0"
}
]

My java code in my backend API (written in Spring Boot) just prints in this format

incomingLists.stream().forEach(
            obj -> System.out.println(obj.getGroupId()+" **** "+obj.getArtifactId()+" **** "+obj.getVersion())
        );

When i hit this API from my postman, the response is correct- see below print from the code

org.hibernate **** hibernate-validator **** 5.3.6.Final
org.projectlombok **** lombok **** 1.16.22

whereas when i hit the same API from my gradle task, my java code prints it this way.. because the inner objects are null

null **** null **** null
null **** null **** null

Am not sure if am missing anything

arundotin commented 6 years ago

I solved this !! the request.body as shown above is just sending the dependencyListas a list object itself and not as a String as expected by the API.

so i changed it to below way and it worked as expected :)

post {
        request.body = dependencyList.toString()
        response.success {
            println "Success"
        }
    }