ProxymanApp / Proxyman

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

Trouble triggering scripts from iOS simulator #1950

Closed ben-codes-five closed 4 months ago

ben-codes-five commented 4 months ago

Description

I am doing iOS development and monitoring network traffic using Proxyman. I can see the network calls in Proxyman, suggesting I have the certificates set up correctly, however, I am trying to modify the response for an endpoint using scripts but the scripts aren't being triggered.

For context, the endpoint is a graphql endpoint and below is my script:

const file = require("mockdata.json");

async function onRequest(context, url, request) {
  // console.log(request);
  console.log(url);

  // 1. Extract the queryName from the request
  var queryName = request.body.query.match(/\S+/gi)[1].split('(').shift();

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

  // Done
  return request;
}

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

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

    // 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;
  }

  // Done
  return response;
}

I can confirm the script works because I am able to trigger the script by rerunning the request within Proxyman, however, I am unable to trigger the script from sending a request from my iOS app.

Any idea what the issue is? Smells like either an internal bug or a configuration issue.

Environment

NghiaTranUIT commented 4 months ago

however, I am trying to modify the response for an endpoint using scripts but the scripts aren't being triggered.

If you don't see any log, it means the Script URL doesn't match your URL.

Can you show me the script URL Rule you're using?


Your script is an old way to match with a GraphQL Query Name. If you're using v5.0.0, there is an easier way:

  1. Call your GraphQL on iPhone -> Make sure you can see the traffic on Proxyman macOS
  2. Right Click on this request -> Tools -> Scripting
  3. ✅ Proxyman automatically creates a matched rule with a given GraphQL QueryName.
  4. Use your new script
const file = require("mockdata.json");

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

response.headers["Content-Type"] = "application/json";
    response.body = file;

  // Done
  return response;
}

Screenshot 2024-02-28 at 08 29 53

NghiaTranUIT commented 4 months ago

Or you can map to a local file directly:

async function onResponse(context, url, request, response) {
  // console.log(response);
  response.headers["Content-Type"] = "application/json";
  response.bodyFilePath = "~/Desktop/my_response.json"

  // Done
  return response;
}
ben-codes-five commented 4 months ago

I was able to get it to work! However, not by importing my json file. I had to map the local file directly. Thanks for your help!

NghiaTranUIT commented 4 months ago

@ben-codes-five Glad to know you made it. The Map Local Tool works fine too 👍

ben-codes-five commented 4 months ago

Reopening because a colleague of mine is having the same issue but can't seem to figure out what the issue is.

jmittelstaedt12 commented 4 months ago

Similar to @ben-codes-five I'm unable to execute my script through requests made from simulator. Only on repeat requests through the proxyman window can I get the script to run. Heres my match:

image

NghiaTranUIT commented 4 months ago

@jmittelstaedt12 can you follow this steps:

  1. Make a HTTP Request first on your iOS Simulator -> Verify you can see the request on Proxyman first
  2. Right Click on this request -> Tools -> Scripting (No need to change any thing, it already works)
  3. Re-sent your request again -> Check if you can see the console.log() from the Scripting ?

May I ask: Is your iOS app a native iOS app or React-Native ?

NghiaTranUIT commented 4 months ago

Here is the video: It works fine.

https://github.com/ProxymanApp/Proxyman/assets/5878421/93e79918-4189-4815-b846-56797721f4b8

jmittelstaedt12 commented 4 months ago

@NghiaTranUIT resolved the issue. My problem seemed to be that I had my VPN on, as its now working with it turned off