Michael
Michael

Reputation: 42050

How to access request body in "onResourceRequested" callback in PhantomJS?

I would like to access the request body of HTTP POST requests in the callback registered with onResourceRequested (I didn't find it in the documentation).

I would like to do something like this:

page.onResourceRequested = function(requestData, networkRequest) {
  var body = networkRequest.body // how to do that ?
  console.log(body)
}

How can I access the body of the request in the onResourceRequested callback ?

Upvotes: 2

Views: 1096

Answers (1)

Davy
Davy

Reputation: 81

The request body of POST requests are stored in the postData attribute of requestData object. You can retrieve it like so:

page.onResourceRequested = function(requestData, networkRequest) {
  var body = networkRequest.postData
  console.log(body)
}

To note, there doesn't seem to currently be a way to retrieve the request body for any other request methods such as PUT or PATCH.

Upvotes: 1

Related Questions