microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.86k stars 3.66k forks source link

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

Open Sakshamrameshhudiya opened 2 years ago

Sakshamrameshhudiya commented 2 years ago

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.

mxschmitt commented 2 years ago

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?

Sakshamrameshhudiya commented 2 years ago

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

mxschmitt commented 2 years ago

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

Sakshamrameshhudiya commented 2 years ago

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

mxschmitt commented 2 years ago

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.

Sakshamrameshhudiya commented 2 years ago

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.

mxschmitt commented 2 years ago

It's not possible yet.

Sakshamrameshhudiya commented 2 years ago

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

mxschmitt commented 2 years ago

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.

mstepin-edt commented 1 year ago

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.

tepmehatop commented 1 year ago

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), }

mstepin-edt commented 1 year ago

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.

mikekol commented 1 year ago

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.

sergiofbsilva commented 1 year ago

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

BogachevaN commented 1 year ago

Hi! And we really need it! 🙏

marcosvfranco commented 1 year ago

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

lucy-mfv commented 1 year ago

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

tongilcoto commented 1 year ago

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 ....

marcosvfranco commented 1 year ago

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

lucy-mfv commented 1 year ago

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

tongilcoto commented 1 year ago

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

lucy-mfv commented 1 year ago

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

tongilcoto commented 1 year ago

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

rohit-walia commented 1 year ago

+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");
marcusNumminen commented 10 months ago

+1 for this!

jcarlsonautomatiq commented 10 months ago

+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

judiths13 commented 9 months ago

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

odonnell-anthony commented 9 months ago

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

mitchismycoffeename commented 8 months ago

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.

AnnaRennie commented 8 months ago

+1 vote for having this feature

asmyshlyaev177 commented 5 months ago

+1

Kranael commented 5 months ago

+1 here! Pls add this feat! @mxschmitt

Dement57 commented 5 months ago

+1

Valiantsin2021 commented 4 months ago

+1

kazwata commented 2 months ago

+1

mfaisalkhatri commented 1 month ago

+1

grajesh690 commented 1 month ago

+1 having this feature would help us to have better control and back tracking.

AshConnolly commented 3 weeks ago

+1