Duxa
Duxa

Reputation: 75

Send raw body data with HTTPBuilder

I need to make POST request with raw body data. How can I do it with HTTPBuilder? In postman it works fine when I choose body type raw and Postman Collections looks like this:

{
    "info": {
        "_postman_id": "id",
        "name": "Requests",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "name",
            "request": {
                "method": "POST",
                "header": [],
                "body": {
                    "mode": "raw",
                    "raw": "\"Check\"",
                    "options": {
                        "raw": {
                            "language": "json"
                        }
                    }
                },
                "url": {
                    "raw": "blabla.123.456.net",
                    "host": [
                        "blabla",
                        "123",
                        "456",
                        "net"
                    ]
                }
            },
            "response": []
        }
    ],
    "protocolProfileBehavior": {}
}

But when I try to send POST request with groovy HTTPBuilder, server returns me, that there is no data in body. How to write a request with raw body? I tried like this:

 def http = new HTTPBuilder("https://${address}")    
    http.setClient(HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build())
    try {
        http.request(POST) {      
                body = "Some text"
            response.success = { resp, reader ->                
                log.debug(resp.statusCode)
                
            }

Upvotes: 0

Views: 360

Answers (0)

Related Questions