playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] Allow accessing request body and headers when performing http request

Open Sakshamrameshhudiya opened this issue 3 years ago • 57 comments

I am trying invoking an API via playwright and I am setting extraheaders via playwright = Playwright.create(); return playwright.request().newContext(new APIRequest.NewContextOptions() .setBaseURL(TeamCityEnvironmentVariable("APIBaseUrl")) .setExtraHTTPHeaders(headers));

and also adding request body to it via pojo classes but I am not able to see what headers and request body is set onces response is given by API.

Sakshamrameshhudiya avatar Aug 04 '22 06:08 Sakshamrameshhudiya

what is pojo?

I'm not completely following this request, could you give us an example what you are trying to do?

You want to see which extraHttpHeaders are set on a request context?

mxschmitt avatar Aug 04 '22 07:08 mxschmitt

Hey Team, I actually want to debug what exact request body I am setting in json format in APIRequestContext

Sakshamrameshhudiya avatar Aug 04 '22 07:08 Sakshamrameshhudiya

what do you mean with json format? Could you provide a code example?

mxschmitt avatar Aug 04 '22 07:08 mxschmitt

by json format i mean i want to know what request is set in request context ? example : { "teamIds": "99790", "effectiveDate": "2020-01-01" }

Sakshamrameshhudiya avatar Aug 04 '22 08:08 Sakshamrameshhudiya

Fair enough, so we can make a feature request out of it and call it "Access request object when performing http requests". This would allow you to access headers, body, etc.

mxschmitt avatar Aug 04 '22 08:08 mxschmitt

can you please give me and code example in java so as to how fetch these details or if there is some link to refer or something in playwright docuement.

Sakshamrameshhudiya avatar Aug 04 '22 08:08 Sakshamrameshhudiya

It's not possible yet.

mxschmitt avatar Aug 04 '22 08:08 mxschmitt

Are we thinking of implementing this? I feel this should be a must for debugging if the request body or any other parameter is incorrectly set. If possible, can we also add a logging or trace feature to hit specific requestcontext API. If these features are added this would make it really powerful and a competitor to rest assured. Thanks

Sakshamrameshhudiya avatar Aug 04 '22 08:08 Sakshamrameshhudiya

Usually you already know which request body you pass into, so you kinda have it. Mostly you care about the response instead, not saying its a bad feature to have, depending on the upvotes we'll implement it.

mxschmitt avatar Aug 04 '22 08:08 mxschmitt

Agree that this would be nice for debugging. According to your docs its already possible for page: PW docs say:

response.request()
Added in: v1.8
returns: <[Request](https://playwright.dev/docs/api/class-request)>[#](https://playwright.dev/docs/api/class-response#response-request-return)
Returns the matching [Request](https://playwright.dev/docs/api/class-request) object.

Could be great for API debugging so I can be sure my headers have worked correctly, especially if the body or headers passed to APIRequestContext.post() have runtime variables. Currently I cant even retrieve the headers or body from the APIRequestContext object, let alone the APIResponse object.

mstepin-edt avatar Nov 15 '22 07:11 mstepin-edt

Hi, The same problem, I did not find a solution how to log the body of the request, exactly the part that was sent, not the response body.

Previously used axios for request in playwright, now using pure playwright. a Axios had a method for logging what was sent, what headers and body. "axios.interceptors.request.use..."

It helps to debug the code, very convenient. For example, I send a POST request where there is a set of variables that are requested in the process for data from other places. Ultimately, there is no understanding of what exactly is sent in the body.

{ "id": 0, "category": { "id": 0, "name": await Users.getUsers(request, 'Andy Warhol', 'rw', token),}, "name": nameState, "tags": [ { "id": 0, "name": nameStateTags } ], "status": await States.getCurrentState(request, 'create', token), }

tepmehatop avatar Nov 18 '22 10:11 tepmehatop

yeah for my previous project we went with supertest for api calls. We made that design decision over 1 1/2 years ago. Now new job and new project, I thought I'd give the native PW api requests another go, thinking they have improved in some way. I was wrong, and will be using supertest again in the new project.

mstepin-edt avatar Nov 18 '22 22:11 mstepin-edt

Another +1 for "please add this". I can log in via the API I'm supposed to test in literally every other language and dialect of JavaScript/TypeScript I've tried, but can't make it work in a Playwright API test and I can't even make sure that it's sending the data I asked it to.

To the person earlier who said that we should know what data we're sending - I know what data I'm telling Playwright to send, I have no idea what data Playwright is actually sending. That's why we need this feature.

mikekol avatar Jan 09 '23 18:01 mikekol

Hello 👋 Any news regarding this? We also need this badly 🙏

sergiofbsilva avatar Apr 11 '23 11:04 sergiofbsilva

Hi! And we really need it! 🙏

BogachevaN avatar Apr 21 '23 16:04 BogachevaN

Up! I also need! unless I will need to use third party like chai-http but I dont like this idea

marcosvfranco avatar Jun 13 '23 11:06 marcosvfranco

Hi! And we really need it too, it's so hard to debug now! 🙏

lucy-mfv avatar Jun 25 '23 15:06 lucy-mfv

yes, please, it is a major debug feature!!! How do we vote for this feature to be implemented? I am assuming it is not just leaving a comment here ....

tongilcoto avatar Jul 19 '23 13:07 tongilcoto

Hey fellas, I realized that we can get this info on Trace Viewer! Just go to the line of the API request and you will have all the info about what we need

marcosvfranco avatar Jul 19 '23 14:07 marcosvfranco

Yep, we can use Trace viewer for trace fail script. It's very helpful

lucy-mfv avatar Jul 20 '23 04:07 lucy-mfv

Trace viewer is for UI testing, right? Our problem is for API testing, we just have the "request" response, not the full browser

tongilcoto avatar Jul 20 '23 09:07 tongilcoto

@tongilcoto Trace viewer can use for api too, please try to use this for api

lucy-mfv avatar Jul 22 '23 03:07 lucy-mfv

Thank you very much @lucy-mfv I have tried it! looks quite good!!! but this is not valid for runtime tasks, i.e. programmatically accessing request data or debugging

tongilcoto avatar Jul 26 '23 08:07 tongilcoto

+1 for access to request context for better debugging. I'm getting a "415 Unsupported media type" http error from a POST request and I can not get the request header field "content-type" to verify if it's correctly being set to 'application/json'.

And because these APIs are being executed against the BrowserContext without actually opening a new Page object, TraceViewer is not helpful either.

Pesudo code:

BrowserContext ctx = PlaywrightResourceFactory.BrowserObj.getInstance().newContext();
APIResponse resp = ctx.request().post("/api/login");

rohit-walia avatar Aug 13 '23 01:08 rohit-walia

+1 for this!

marcusNumminen avatar Dec 18 '23 10:12 marcusNumminen

+1 a lot of different libraries will get you the actual request object back on a response. It would be great to have that for the response on generated by the APIContext.request as well

jcarlsonautomatiq avatar Jan 02 '24 14:01 jcarlsonautomatiq

+1, would definitely be useful to be able to access the request body/headers/etc outside of the trace viewer

judiths13 avatar Jan 22 '24 14:01 judiths13

+1 please provide this, it such an odd thing to be lacking

odonnell-anthony avatar Jan 30 '24 22:01 odonnell-anthony

Another vote to please add! I can do it with trace viewer, but it's a very heavy-weight solution and, as others have pointed out, this is pretty standard on other API tools. But I want to do my UI testing and API testing in concert, and I could really use this information for debugging. Thanks.

mitchismycoffeename avatar Mar 04 '24 22:03 mitchismycoffeename

+1 vote for having this feature

AnnaRennie avatar Mar 05 '24 13:03 AnnaRennie