Skip to content

feat: add request and response body on network tool #251

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

Closed

Conversation

bun913
Copy link

@bun913 bun913 commented Apr 23, 2025

Background

Closes #271

Details

  • I modified network.ts to enable it to return request and response body information
    • And it returns only xhr or fetch request, because the context window is not so big, so big CSS and fonts are a waste of space
  • I added the test on network.spec.ts

@bun913
Copy link
Author

bun913 commented Apr 23, 2025

@microsoft-github-policy-service agree

@bun913
Copy link
Author

bun913 commented Apr 23, 2025

I've already tried it on my test products.
It's a help.

Thanks to it, I can investigate API calls and order AI to write tests in VSCode only.
I believe that it will help people who are writing automation tests (API tests and UI automation tests)

Copy link
Member

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like adding the request/response bodies, but I don't think we should limit the tool result to only XHR/fetch requests. Could you trim down the PR to just that? Maybe you could conditionally only include the bodies for XHR/fetch requests to save on some tokens.

@bun913 bun913 requested a review from Skn0tt April 28, 2025 09:48
@bun913
Copy link
Author

bun913 commented Apr 28, 2025

@Skn0tt
Thank you for your advice. As you mentioned, it is better to include all requests.
So, I changed it like below.

  • XHR or fetch
    • Include Request and Response Body
  • except them
    • Include the [METHOD] URL only

What do you think about that? Thank you in advance.

return {
code: [`// <internal code to list network requests>`],
code: ['// <internal code to list all network requests>'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9341e64

I revert as above

@@ -24,15 +24,16 @@ const requests = defineTool({

schema: {
name: 'browser_network_requests',
description: 'Returns all network requests since loading the page',
description: 'Returns all network requests since loading the page, if the request is a xhr or fetch, it will return the request body and response body',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we need to be this explicit here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I revert this line

if (resourceType === 'xhr' || resourceType === 'fetch')
return true;
const headers = request.headers();
if (headers['x-requested-with']?.toLowerCase() === 'xmlhttprequest')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't a standard header, I don't feel comfortable relying on it. please remove this.

return true;
return request.isNavigationRequest() === false &&
(headers.accept?.includes('application/json') ||
headers['content-type']?.includes('application/json'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just check if there's a body, not what its content type is. if the request has a body, that's always interesting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the conditions like the below

996df4f

const headers = request.headers();
if (headers['x-requested-with']?.toLowerCase() === 'xmlhttprequest')
return true;
return request.isNavigationRequest() === false &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a form submission could be a navigation request, but still has a super interesting body. let's not check for it being a nav request.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the conditions same as the above comment.

  • Is application/json request?
  • And if there is body, the tool includes it in the result

if (postData)
result.push(`Request Body: ${postData}`);
if (response) {
const body = await response.body();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use response.text()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bun913 bun913 requested a review from Skn0tt April 28, 2025 10:41
@bun913
Copy link
Author

bun913 commented Apr 28, 2025

@Skn0tt

Sorry for bothering you.
I fixed and simplified the overall code as you mentioned.
Please check them at your earliest convenience.

@bun913
Copy link
Author

bun913 commented Apr 28, 2025

I fixed some lint errors and ran npm run lint to check them.

@praveen2399
Copy link

I am looking for this feature. Can this be available in next release ?

@bun913
Copy link
Author

bun913 commented Apr 29, 2025

@praveen2399
Thank you. I'm also finding that the response body is beneficial when the request is in JSON format.

@Skn0tt

Could you please re-review at your earliest convenience?

@pavelfeldman
Copy link
Member

Thank you. I'm also finding that the response body is beneficial when the request is in JSON format.

That's hardly an excuse to flood the chat with the bodies. I'll close this for now as non-practical. I think we should log network into files (har) and expose them as resources for the next gen clients though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Network request] Add request and response body on network tool
4 participants