EchoScript Examples
Let’s embark on a journey to master EchoScript through illustrative examples.
Basic Examples
method
Example: change request method to POST
query
Example A: change URL query parameter page=2
Example B: change URL query parameter page
to page+3
header
Example A: change HTTP request header User-Agent
and Head-Example
Example B: change HTTP response header Head-Example
cookie
Example A: change HTTP request Cookie
: DemoID=ABCD1234; DemoKey=XYZ789
Example B: change HTTP response Set-Cookie
:
status code
Example: change HTTP response status code to 404
redirect
Example: redirect to https://demo.echolabx.com/example
with
status code
302,
which indicates that the resource requested has been temporarily moved to the URL given by the Location header.
Body Examples
The returned body
field of functions on_request and on_response has the same structure.
body: string
When returned body
is a string
, the final body content is the same with this string.
It’s your responsibility to make sure the body string format is suitable for the Content-Type header
.
Example: Modify response body text by inserting a message before EchoProxy
.
Create a Map Rule with following settings:
MapRemote, GET, Equals, Enabled ✅
URL:https://demo.echolabx.com/
Default body content:Hi: EchoProxy
Visit URL https://demo.echolabx.com/ again, the output is following:
body: file
When returned body
is a file
, EchoScript will load the file content as a text string,
and the final body content is the same with the file
content.
Example A: Load file content to response body. Create a Map Rule with following settings:
MapRemote, GET, Equals, Enabled ✅
URL:https://demo.echolabx.com/
Default body content:Hi: EchoProxy
Create a new file at location ~/workspace/input.txt
with following content:
Visit URL https://demo.echolabx.com/ again, the output will be the same with the above file.
Example B: Load file content to response body as an attachment
.
Update the Map Rule in Exampe A with only EchoScript changed:
Visit URL https://demo.echolabx.com/ again, the web browser will trigger file downloading with name input.txt
.
The above script is equivalent to the following code.
body: map
The req.body
and res.body
arguments are automatically parsed as map
when Content-Type
is in the following list.
Accordingly, the returned body
should be a map
as well.
application/json
application/x-www-form-urlencoded
multipart/form-data
When the returned body
is a map
, the final body content is encoded in json
format by defualt.
The relationship between Content-Type
and final body content encoding format:
Content-Type | Final body encoding Format |
---|---|
application/json | json |
application/x-www-form-urlencoded | urlencoded |
multipart/form-data | form-data |
other | json |
Body map Examples
application/json
Example: Merge response body with Content-Type: application/json
.
Suppose the response data is in the following format.
We want to change the value of name
to EchoProxy 1.0.0
, and add fields list
and query.count
.
Create a Map Rule with following settings:
MapRemote, GET, Equals, Enabled ✅
URL:https://demo.echolabx.com/json?page=3
Visit URL https://demo.echolabx.com/json?page=3 again, the output is following:
application/x-www-form-urlencoded
Example: Merge request body with Content-Type: application/x-www-form-urlencoded
.
Create a Map Rule with following settings:
MapRemote, POST, Equals, Enabled ✅
URL:https://demo.echolabx.com/form-post
Send a POST request to https://demo.echolabx.com/form-post
with EchoSend .
You can check the updated request body in tab ReqBody.
multipart/form-data
Example: Merge request body with Content-Type: multipart/form-data
.
Create a Map Rule with following settings:
MapRemote, POST, Equals, Enabled ✅
URL:https://demo.echolabx.com/upload-post
Send a POST request to https://demo.echolabx.com/upload-post
with EchoSend .
You can check the updated request body in tab ReqBody.
The above script is equivalent to the following code.