Wiremock: Stubbing application/json response without having to escape double quotes in response body

Created on 6 Dec 2014  路  6Comments  路  Source: tomakehurst/wiremock

How can I stub application/json response without having to escape all the double quotes, i.e.:
I want to get rid off all of those escape chars in response.body contents:

{
    "request": {
            "method": "GET",
            "url": "/some/thing"
    },
    "response": {
            "status": 200,
            "body": "{\"id\": \"1\", \"type\": \"something\"}",
            "headers": {
                    "Content-Type": "application/json"
            }
    }
}

Most helpful comment

You can always use jsonBody instead of body and have much cleaner code:

{
    "request": {
            "method": "GET",
            "url": "/some/thing"
    },
    "response": {
            "status": 200,
            "jsonBody": {"id": "1", "type": "something"},
            "headers": {
                    "Content-Type": "application/json"
            }
    }
}

All 6 comments

You can put the body in a separate file using bodyFilePath.

@tomakehurst How does that work precisely?

Like this?

{
    "request": {
            "method": "GET",
            "url": "/some/thing"
    },
    "response": {
            "status": 200,
            "bodyFilePath": "path/to/file",
            "headers": {
                    "Content-Type": "application/json"
            }
    }
}

I got it to work!

For the next person needing this: at least on wiremock 2.8 the property is actually called bodyFileName, not bodyFilePath.

I get away with :

"body": "{'id': '1', 'type': 'something'}", or
"body": '{ ... }'

You can always use jsonBody instead of body and have much cleaner code:

{
    "request": {
            "method": "GET",
            "url": "/some/thing"
    },
    "response": {
            "status": 200,
            "jsonBody": {"id": "1", "type": "something"},
            "headers": {
                    "Content-Type": "application/json"
            }
    }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wujimin picture wujimin  路  4Comments

OllyAndJo picture OllyAndJo  路  5Comments

j3t picture j3t  路  5Comments

samdnz picture samdnz  路  3Comments

leoluz picture leoluz  路  5Comments