Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onResponse changing values in selected attributes - doesn’t work #2210

Open
MalTa07 opened this issue Dec 8, 2024 · 2 comments
Open

onResponse changing values in selected attributes - doesn’t work #2210

MalTa07 opened this issue Dec 8, 2024 · 2 comments
Assignees
Labels
✅ Done Ticket is addressed and fixed. question Further information is requested

Comments

@MalTa07
Copy link

MalTa07 commented Dec 8, 2024

I am a tester with no programming skills. I use Maplocal or breakpoints on a daily basis. I noticed the scripts tab and wondered what it could be useful to me.
I am thinking about the case when I want to modify one value in the response without having to manually set up a breakpoint (situation when I need to have fresh /unique data in the response, but I need to change something specific)
As I do not know much about programming I asked the gpt chat to create such a script, unfortunately, despite many attempts/changes/analysis of errors, the data is not changed in the final response.
So, there is a simple response body (Json) with 3 attributes {"messages":10,"id":115,"card":0} , the goal is to change the response body for only messages value.

This is what i get from chatgpt:

function onResponse(context) {
    if (context.response && context.response.body) {
        let body = context.response.body;
        try {
            let json = JSON.parse(body);
            json.messages = 1;
            body = JSON.stringify(json);
            context.response.body = body;
            console.log("Modified response body:", body);
        } catch (e) {
            console.error("Error parsing JSON:", e);
        }
    } else {
        console.log("Response body is empty or undefined.");
    }
    return context.response;
}

and this is na answer :
Response body is empty or undefined.

@MalTa07 MalTa07 added the question Further information is requested label Dec 8, 2024
@NghiaTranUIT
Copy link
Member

This func is wrong. It's not how the Script works

function onResponse(context) {}

You should use:

function onRequest(context, url, request) {
    // Set JSON content
    request.headers["Content-Type"] = "application/json";
    
    // Get Request body
    var jsonBody = request.body;
    
    // Modify data
    jsonBody["name"] = "Proxyman";
    jsonBody["flatform"] = "macOS";
    jsonBody["info"] = { 
                        "website": "proxyman.io",
                        "region": "Earth"
                       };
    // Set it again        
    request.body = jsonBody;
    return request;
}

You can find all useful snippet code at https://docs.proxyman.io/scripting/snippet-code#json-body

@MalTa07
Copy link
Author

MalTa07 commented Dec 9, 2024

the situation I needed was to change the response, but this was very helpful! the final script is working <3
and it looks like this

function onResponse(context, url, request) {
    // Set JSON content
    response.headers["Content-Type"] = "application/json";
    
    // Get Request body
    var jsonBody = response.body;
    
    // Modify data
    jsonBody["messages"] = 1;

    // Set it again        
    response.body = jsonBody;
    return response;
}

THX! my work will be much easier from now on.

@NghiaTranUIT NghiaTranUIT added the ✅ Done Ticket is addressed and fixed. label Dec 12, 2024
@NghiaTranUIT NghiaTranUIT self-assigned this Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✅ Done Ticket is addressed and fixed. question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants