ProxymanApp / Proxyman

Modern. Native. Delightful Web Debugging Proxy for macOS, iOS, and Android ⚡️
https://proxyman.io
5.57k stars 186 forks source link

No way to filter on request body #780

Open rsaunders100 opened 3 years ago

rsaunders100 commented 3 years ago

Proxyman version? (Ex. Proxyman 1.4.3)

Version 2.16.1 (21610)

We have a universal endpoint, the request body changes not the URL so there is no way to filter on a particular request for rewrite or a breakpoint.

Screenshot 2021-02-05 at 21 01 26

NghiaTranUIT commented 3 years ago

Hi @rsaunders100, we'd not introduce the "Request Body Matching" because it could make the UI more complicated.

To resolve it, I'd highly suggest using ReWrite. Here is an example to solve your cases:

  1. Create a scripting rule (URL = https://example.com)
  2. Use the following script to match the Request and then mapping the Response (Please read the comment)
// Import file from More Button -> Import JSON or Other files 
const file = require("@users/B02D96D5.default_message_32E64A5B.json");

function onRequest(context, url, request) {

  // 1. Extract the name from Request Body
  // request.body is a JSON, so you can get whatever you want
  var queryName = request.body["name"]

  // 2. Save to sharedState
  sharedState.queryName = queryName

  // Done
  return request;
}

function onResponse(context, url, request, response) {

  // 3. Check if it's the request we need to map
  if (sharedState.queryName == "getUser") {

    // 4. Import the local file by Action Button -> Import
    // Get the local JSON file and set it as a body (like Map Local)
    response.headers["Content-Type"] = "application/json";
    response.body = file;

    // Or change header, status code, body like Breakpoint Tool
    // Example: https://docs.proxyman.io/scripting/snippet-code#2-common-on-request-and-response
  }

  // Done
  return response;
}
  1. Profit 😄
Screen Shot 2021-02-06 at 09 17 33

Notes

rsaunders100 commented 3 years ago

Thanks! Very helpful.

Is the sharedState variable specific to the request response pair? Or is it global for all requests?

NghiaTranUIT commented 3 years ago

Yes, it's only available in this request-response pair (not global variable for all requests). You can read more at https://docs.proxyman.io/scripting/environment-variables @rsaunders100