Is there a way to map local in proxyman based off of parameters attached to the body of a url?

Viewed 85

I have a url:

https://cn.company.com/appv2/search

and want to have a different map local depending on the request coming with a different parameter in the body (i.e. it is NOT attached to the url like https://cn.company.com/appv2/search?cursor=abc. Instead it is in the body of the request { cursor: abc }.

Any idea on if this can be done in proxyman?

I basically want to be able to stub pagination through the proxy without waiting on a server implementation. So I'd have no cursor on the first request, server would return a cursor and then use that on the next request and get a different response from server on the request so that I can test out the full pagination flow.

1 Answers

Yes, it can be solved with the Scripting from Proxyman app.

  1. Use Scripting to get the value of the request body
  2. If it's matched, use Scripting to mimic the Map Local (Mock API also supports)

Here is the sample code and how to do it:

  1. Firstly, call your request and make sure you can see the HTTPS Response
  2. Right-Click on the request -> Tools -> Scripting
  3. Select the Mock API checkbox if you'd like a Mock API
  4. Use this code
/// This func is called if the Response Checkbox is Enabled. You can modify the Response Data here before it goes to the client
/// e.g. Add/Update/Remove: headers, statusCode, comment, color and body (json, plain-text, base64 encoded string)
///
async function onResponse(context, url, request, response) {
  
  // get the value from the body request
  var cursorValue = request.body["cursor"];
  
  // Use if to provide a map local file
  if (cursorValue === "abc") {

    // Set Content Type as a JSON
    response.headers["Content-Type"] = "application/json";

    // Set a Map Local File
    response.bodyFilePath = "~/Desktop/my_response_A.json";

  } else if (cursorValue === "def") {
    // Set Content Type as a JSON
    response.headers["Content-Type"] = "application/json";

    // Set a Map Local File
    response.bodyFilePath = "~/Desktop/my_response_B.json";
  }

  // Done
  return response;
}

Reference

Related