Get Raw Post Data

How to get body of a POST in php?

To access the entity body of a POST or PUT request (or any other HTTP method):

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

Also, the STDIN constant is an already-open stream to php://input, so you can alternatively do:

$entityBody = stream_get_contents(STDIN);

From the PHP manual entry on I/O streamsdocs:

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

Specifically you'll want to note that the php://input stream, regardless of how you access it in a web SAPI, is not seekable. This means that it can only be read once. If you're working in an environment where large HTTP entity bodies are routinely uploaded you may wish to maintain the input in its stream form (rather than buffering it like the first example above).

To maintain the stream resource something like this can be helpful:

<?php

function detectRequestBody() {
$rawInput = fopen('php://input', 'r');
$tempStream = fopen('php://temp', 'r+');
stream_copy_to_stream($rawInput, $tempStream);
rewind($tempStream);

return $tempStream;
}

php://temp allows you to manage memory consumption because it will transparently switch to filesystem storage after a certain amount of data is stored (2M by default). This size can be manipulated in the php.ini file or by appending /maxmemory:NN, where NN is the maximum amount of data to keep in memory before using a temporary file, in bytes.

Of course, unless you have a really good reason for seeking on the input stream, you shouldn't need this functionality in a web application. Reading the HTTP request entity body once is usually enough -- don't keep clients waiting all day while your app figures out what to do.

Note that php://input is not available for requests specifying a Content-Type: multipart/form-data header (enctype="multipart/form-data" in HTML forms). This results from PHP already having parsed the form data into the $_POST superglobal.

What is the raw POST data?

An HTTP request consists of two parts. A set of headers and a body.

The headers include things like the URL being requested and caching control helpers (such as "I have a version of this from yesterday, only give me a new one if there are changes, OK?").

The body may or may not appear depending on the type of request. POST requests have bodies.

The body can be in any format the client likes. One of the headers will tell the server what the format is.

There are a couple of formats used by HTML forms, and PHP knows how to parse these and put the data into $_POST.

If the data is in another format, such as JSON, or if the data doesn't conform to PHP's quirks (such as the rules for having [] on the end of keys with the same name) then you might want to access the data directly so you can parse it yourself.

That is the raw POST data.

Getting raw POST data from Web API method

For anyone else running into this problem, the solution is to define the POST method with no parameters, and access the raw data via Request.Content:

public HttpResponseMessage Post()
{
Request.Content.ReadAsByteArrayAsync()...
...

Get raw post data

Direct answer: you can not do that. PHP insists on parsing it itself, whenever it sees the multipart/form-data Content-Type. The raw data will not be available to you. Sadly. But you can hack around it.

I hit a similar problem, a partner was sending incorrectly formatted data as multipart/form-data, PHP could not parse it and was not giving it out so I could parse it myself.

The solution? I added this to my apache conf:

<Location "/backend/XXX.php">
SetEnvIf Content-Type ^(multipart/form-data)(.*) NEW_CONTENT_TYPE=multipart/form-data-alternate$2 OLD_CONTENT_TYPE=$1$2
RequestHeader set Content-Type %{NEW_CONTENT_TYPE}e env=NEW_CONTENT_TYPE
</Location>

This will change the Content-Type of incoming request to XXX.php from multipart/form-data to multipart/form-data-alternate, which is enough to block PHP from trying to parse it

After this you can finally read the whole raw data from php://input and parse it yourself.

It is ugly, but I have not found a better or in fact any other solution - short of asking the partner to fix their side.

NB! When you do what I described here, $_FILES will be empty.

Accessing raw POST data in Express

In order to read the body of a post request you need body-parser.
If you also need to parse multipart/form-data you need multer.

after you npm install them:

const express = require('express');
const multer = require('multer');
const bodyParser = require('body-parser');
const upload = multer();
const app = express();

// create application/json parser
app.use(bodyParser.json());

// create application/x-www-form-urlencoded parser
app.use(bodyParser.urlencoded({ extended: false }));

function handleRequest(req, res) {
console.log('\n-- INCOMING REQUEST AT ' + new Date().toISOString());
console.log(req.method + ' ' + req.url);
console.log(req.body);
res.send('Hello World!');
}

app.post('/*', upload.any(), (req, res) => handleRequest(req, res));
app.all('/*', (req, res) => handleRequest(req, res));
app.listen(3000, () => console.log('Example app listening on port 3000!'));

Get raw POST body in Python Flask regardless of Content-Type header

Use request.get_data() to get the raw data, regardless of content type. The data is cached and you can subsequently access request.data, request.json, request.form at will.

If you access request.data first, it will call get_data with an argument to parse form data first. If the request has a form content type (multipart/form-data, application/x-www-form-urlencoded, or application/x-url-encoded) then the raw data will be consumed. request.data and request.json will appear empty in this case.

Get raw POST payload in Flask

This is not really a Flask problem, you are using the wrong curl options.

The -d switch should only be used for form data. curl automatically will set the Content-Type header to application/x-www-form-urlencoded, which means that Flask will load the raw body content and parse it as a form. You'll have to set a different Content-Type header manually, using -H 'Content-Type: application/octet-stream' or another mime-type more appropriate to your data.

You also want to use --data-binary, not -d (--data), as the latter also tries to parse the content into key-value fields and will remove newlines:

curl -X POST -H 'Content-Type: application/octet-stream' \
--data-binary "Separate account charge and opdeducted fr" \
http://192.168.50.8/text

Get raw data sent by postman on php

$_POST array is for form data requests.

You need to read json from input:

$jsonData = json_decode(file_get_contents("php://input"), true);


Related Topics



Leave a reply



Submit