How to Retrieve Request Payload

How to retrieve Request Payload

If I understand the situation correctly, you are just passing json data through the http body, instead of application/x-www-form-urlencoded data.

You can fetch this data with this snippet:

$request_body = file_get_contents('php://input');

If you are passing json, then you can do:

$data = json_decode($request_body);

$data then contains the json data is php array.

php://input is a so called wrapper.

php://input is a read-only stream that allows you to read raw data
from the request body. In the case of POST requests, it is preferable
to use php://input instead of $HTTP_RAW_POST_DATA as it does not
depend on special php.ini directives. Moreover, for those cases where
$HTTP_RAW_POST_DATA is not populated by default, it is a potentially
less memory intensive alternative to activating
always_populate_raw_post_data. php://input is not available with
enctype="multipart/form-data".

How to retrieve Request Payload from Chrome DevTools Network?

you can use https://webdriver.io/docs/api/browser/mock.html and get postData from the mock like this yourMock.calls[0].postData

Python Is it possible to get request payload from a post request?

In post request, you have to add data which is sent by the server as payload as json response and captcha Token is also a part of payload data. Here is an example how to pull data (name, price) from api calls json response as post method.

import requests
import json
URL = "https://public-api.pricempire.com/api/search/items"

body= {"page":1,"priceMin":0,"orderBy":"price_desc","captchaToken":"03AGdBq271Msp7k_yCTzgNsheZ1yRqLWykDZL17tIK9_YAVo2uZGc3cLH0sNhuZOFsnymBSAbuzRRo2w_Cy6kEEMxaRxgkuZUlXFcDzRPWgYs-Hy-fV5SpxLjU8rACYW3KwZ8y-js1Dye8weAdMfZSPeEBgQ9YP3zdbaPrUOJAHHmjkpqTxH7vPW-Cd2PXHtZf5NlgVkxCBUKIESAyMJ6FyKdNz_WxYdIJvK4uQa6nBdHxMlmQZx6rUgus65NxZkwTaY3BO36ju68WNerv-fQBqFdIz_6jUPfav41DYFiApv9O-MbdASQqpS-ma1TG76mQ82OQdzkqqvpZtAksBGa836HzsxfaOecgbZ2YbswAHr1dXxl919DbRnZum4Wr-UUZMQ66j8Iy5UA_g4B3Ir7IxTf50KhTOrNHtqIIYuBR4Vfz6scc5c7XqATeqMoMvL-06wbBWVATSI44","priceMax":200000,"collections":[],"weaponIds":[],"wears":[],"priceProvider":"buff163"}

headers={
'content-type': 'application/json',
'User-Agent':'mozila/5.0/'
}
jsonData=requests.post(URL,headers=headers,data=json.dumps(body)).json()


for item in jsonData['items']:
name= item['name'].replace('★','').replace('|','').strip()
price=item['price']['price']
print(name)
print(price)

Output:

Souvenir AWP  Dragon Lore (Minimal Wear)
100000000
Sticker iBUYPOWER (Holo) Katowice 2014
49999900
Sticker Titan (Holo) Katowice 2014
49999800
Souvenir AWP Dragon Lore (Field-Tested)
36388800
Sticker Reason Gaming (Holo) Katowice 2014
30000000
Souvenir AWP Dragon Lore (Battle-Scarred)
23618000
Sport Gloves Pandora's Box (Factory New)
22000000
Sticker Team LDLC.com (Holo) Katowice 2014
16750000
StatTrak™ Ursus Knife Crimson Web (Factory New)
15000000
StatTrak™ Talon Knife Crimson Web (Factory New)
15000000
StatTrak™ Nomad Knife Safari Mesh (Battle-Scarred)
15000000
Survival Knife Crimson Web (Factory New)
14999999
StatTrak™ Stiletto Knife Slaughter (Field-Tested)
14888800
Sport Gloves Vice (Factory New)
13400000
Sticker Vox Eminor (Holo) Katowice 2014
13000000
Sticker Team Dignitas (Holo) Katowice 2014
11886000
StatTrak™ M9 Bayonet Case Hardened (Factory New)
10999900
StatTrak™ Paracord Knife Crimson Web (Factory New)
10000000
StatTrak™ Ursus Knife Fade (Minimal Wear)
10000000
Sport Gloves Slingshot (Factory New)
10000000

Reading Request Payload in PHP

If you need view x-www-form-urlencoded post data, just use

print_r($_POST);

If you need view json post data, use this

$data=json_decode(file_get_contents('php://input'),1);
print_r($data);

HTTP GET with request body

Roy Fielding's comment about including a body with a GET request.

Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

So, yes, you can send a body with GET, and no, it is never useful to do so.

This is part of the layered design of HTTP/1.1 that will become clear again once the spec is partitioned (work in progress).

....Roy

Yes, you can send a request body with GET but it should not have any meaning. If you give it meaning by parsing it on the server and changing your response based on its contents, then you are ignoring this recommendation in the HTTP/1.1 spec, section 4.3:

...if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.

And the description of the GET method in the HTTP/1.1 spec, section 9.3:

The GET method means retrieve whatever information ([...]) is identified by the Request-URI.

which states that the request-body is not part of the identification of the resource in a GET request, only the request URI.

Update

The RFC2616 referenced as "HTTP/1.1 spec" is now obsolete. In 2014 it was replaced by RFCs 7230-7237. Quote "the message-body SHOULD be ignored when handling the request" has been deleted. It's now just "Request message framing is independent of method semantics, even if the method doesn't define any use for a message body" The 2nd quote "The GET method means retrieve whatever information ... is identified by the Request-URI" was deleted. - From a comment

From the HTTP 1.1 2014 Spec:

A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.

Getting request payload from POST request in Java servlet

Simple answer:

Use getReader() to read the body of the request

More info:

There are two methods for reading the data in the body:

  1. getReader() returns a BufferedReader that will allow you to read the body of the request.

  2. getInputStream() returns a ServletInputStream if you need to read binary data.

Note from the docs: "[Either method] may be called to read the body, not both."



Related Topics



Leave a reply



Submit