Open In App

WireMock – Proxying with JSON Mappings

Last Updated : 29 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

WireMock has the power to selectively proxy requests through to other hosts. This supports a proxy setup where requests are by default proxied to another service, but where specific stubs are configured these are returned in place of the remote service’s response. 

WireMock - Proxying with JSON Mappings

 

Let’s say there are 2 services, ServiceA and ServiceB, out of which 1 is available (ServiceA) and another one (ServiceB) is still under development. Now, we want to configure our Wiremock instance in such a way that all the requests coming for ServiceA should be proxied to the actual ServiceA host and all the requests to ServiceB will be served from preconfigured Wiremock responses. Let’s understand the whole concept by the following example. In this article, we will explain how Proxying works in WireMock with JSON Mappings.

Note: To perform Proxying with JUnit Test please refer to this article: WireMock – Proxying with JUnit Test

Step by Step Implementation 

Step 1: Run Your Standalone WireMock Server

Open the folder in your local machine where your WireMock JAR file is present. Open the cmd on that directory and hit the following command in order to run the JAR file.

java -jar WireMockJarFileName

 If everything is fine then as you can see in the below image our WireMock has been started successfully and the default port number is 8080. 

 

Step 2: Open Postman in Your Local Machine

Go to your Postman app and create a new stub request with the following JSON body. 

{
    "request": {
        "method": "GET",
        "urlPattern": "/article/.*"
    },
    "response": {
        "proxyBaseUrl": "http://www.google.com/",
        "status": 200
    }
}

Refer to the below image if you are stuck somewhere. 

 

Just look at the above request body. This will create a mapping in WireMock and will route all the request URLs matching “/article” to host “www.google.com”. That means if you request for the URL “http://localhost:8080/article/12345“, then your request will actually be proxied to the configured host in the WireMock mappings to “http://www.google.com/article/12345“. In the below images you can see we are getting same response in both the cases. 

 

 

Some More Concepts to Know About WireMock Proxying with JSON Mappings

1. Proxy Stub Mappings

Proxy responses are defined in exactly the same manner as stubs, meaning that the same request matching criteria can be used. And this is what we have demonstrated in the above example. The standard syntax for this is as follows in JSON

{
    "request": {
        "method": "GET",
        "urlPattern": "/other/service/.*"
    },
    "response": {
        "proxyBaseUrl": "http://otherhost.com/approot"
    }
}

2. Remove Path Prefix

The prefix of a request path can be removed before proxying the request. The standard syntax for this is as follows in JSON

{
    "request": {
        "method": "GET",
        "url": "/other/service/doc/123"
    },
    "response": {
        "proxyBaseUrl": "http://otherhost.com/approot",
        "proxyUrlPrefixToRemove": "/other/service"
    }
}

3. Additional Headers

It is possible to configure the proxy to add headers before forwarding the request to the destination. The standard syntax for this is as follows in JSON

{
    "request": {
        "method": "GET",
        "urlPattern": ".*"
    },
    "response": {
        "proxyBaseUrl": "http://otherhost.com",
        "additionalProxyRequestHeaders": {
            "User-Agent": "Mozilla/5.0 (iPhone; U; CPU iPhone)"
        }
    }
}

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads