PHP Local Server Invalid Request (Unexpected Eof)

PHP local server Invalid request (Unexpected EOF)

This is a known bug.

From https://bugs.php.net/bug.php?id=60471:

[2011-12-08 15:01 UTC] lolautruche at gmail dot com Description:

------------
From time to time, built-in server with router script logs this kind of stuff :

[Thu Dec 8 13:39:29 2011] 127.0.0.1:50358 Invalid request (Unexpected
EOF)

This happens quite randomly, whatever the script has a close tag or
not, whatever it has a blank line after close tag or not.

And the explanation:

[2012-02-23 18:45 UTC] michal dot pipa dot xsolve at gmail dot com
...

Chromium has feature called "Predict network actions to improve page
load performance" and its enabled by default. It works this way, that
if HTML page has links to some resources, than Chromium opens about
10 TCP connections in advance. And then if browser has less than 10
resources to fetch, unused connections times out after 10 seconds.
And this (empty payload) causes PHP server to display "Invalid request
(unexpected EOF)" message.

It is a harmless error from the PHP built-in server.

laravel 5.4 php artisan serve Invalid request (Unexpected EOF)

I think you are running a Laravel project blog by command php artisan serve and you are browsing your application by localhost:8000 or 127.0.0.1:8000

Your screenshot saying that when you are trying to visit a url which is not defined in your routes/web.php then, you are getting invalid request (Unexpected EOF) error in your cmd.

And of course! in this case when you close cmd then your Laravel app will not run.

So now, if you don't want to run your Laravel app by the following command each time, then you can use Laragon in windows environment.

How to fix Invalid request (Unsupported SSL request)

I solved this later, just in case anyone has a similar problem in the future, I discovered the AppServiceProvider class had a register method where a security interception middleware was registered to force every HTTP request to HTTPS. So I wrote an if-else logic to allow this only on production since my localhost had no SSL certificate.

in app/providers/AppServiceProvider.php

look for the register() method, then implement the logic by checking whether the app is local or not. The isLocal() method checks your .env file for APP_ENV variable and returns true if it is local, so you reconfirm again that, that is set to local and be sure to clear any previous configuration cache.

//check that app is local
if ($this->app->isLocal()) {
//if local register your services you require for development
$this->app->register('Barryvdh\Debugbar\ServiceProvider');
} else {
//else register your services you require for production
$this->app['request']->server->set('HTTPS', true);
}

Unexpected EOF while using graphql

The issue is because the query you're passing might be empty.

For example:

curl -X POST http://localhost:4000/graphql \ 
-H "Content-Type: application/json" \
-d '{"query": "{ user { id } }"}'

works fine.

But if you make something like:

curl -X POST http://localhost:4000/graphql \
-H "Content-Type: application/json" \
-d '{"query": ""}'

You'll get unexpected < EOF >

Also, check GraphQL end of line issue.

browser sent an invalid request to server

When setting the headers you have a space between the header name and the separating colon. That results in Curl using two headers, a Content-Type : application/json header and a Content-Type: application/x-www-form-urlencoded header, which is the default when posting data. The server could identify the content type as "x-www-form-urlencoded" using the second header, however the first header would cause a 400 response, because there should be no space between the header and the colon. From rfc7230, section 3.2.4:

No whitespace is allowed between the header field-name and colon. In
the past, differences in the handling of such whitespace have led to
security vulnerabilities in request routing and response handling. A
server MUST reject any received request message that contains
whitespace between a header field-name and colon with a response code
of 400 (Bad Request).

<!- -!>

Your code should return successful response using the headers below.

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic ZGFpbHlvcmRlcnM6ZHVtbf4fsww=",
"Content-Type: application/json"
));

When debugging such issues, it's best to enable the verbose mode which prints helpful information about the connection.

curl_setopt($ch, CURLOPT_VERBOSE, true);

In your Postman request you're submitting a HTTP_X_MERCHANT_CODE header; you should include that in your Curl request.

$headers = array(
"Authorization: Basic ZGFpbHlvcmRlcnM6ZHVtbXlLZXk=",
"Content-Type: application/json",
"HTTP_X_MERCHANT_CODE: DOC",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

This would fix the "X_Merchant_Code is null" error, but the server would return a new error message: "Expected BEGIN_OBJECT but was BEGIN_ARRAY", pointing at: ...shipment.shipment_items[0].item_content, because the json data does not have the required format. The item_content key is an empty array, which gets encoded as [], but the server expects a json object ({}). We can fix this issue if we use a null object.

$data['services'][0]['service_details'][0]['shipment']['shipment_items'][0]['item_content'] = (object)null;

The complete code:

<?php

$data = [
"request_id" => "835",
"client_name" => "DOC",
"services" => [
[
"service_code" => "REGULAR",
"service_details" => [
[
"service_leg" => "FORWARD",
"service_data" => [
"vendor_name" => "Ekart",
"amount_to_collect" => 1000,
"dispatch_date" => "",
"customer_promise_date" => "",
"delivery_type" => "SMALL",
"source" => [
"address" => [
"first_name" => "AVThamizhmahan",
"address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2" => "Tamil Nadu",
"pincode" => "400066",
"city" => "Villupuram",
"state" => "Tamil Nadu",
"primary_contact_number" => "88888888"
]
],
"destination" => [
"address" => [
"first_name" => "AVThamizhmahan",
"address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2" => "Tamil Nadu",
"pincode" => "400066",
"city" => "Villupuram",
"state" => "Tamil Nadu",
"primary_contact_number" => "88888888"
]
],
"return_location" => [
"address" => [
"first_name" => "AVThamizhmahan",
"address_line1" => "3/1675 ES Garden Vazhudaretty Post Villupuram",
"address_line2" => "Tamil Nadu",
"pincode" => "400066",
"city" => "Villupuram",
"state" => "Tamil Nadu",
"primary_contact_number" => "88888888"
]
]
],
"shipment" => [
"client_reference_id" => "ABCD",
"tracking_id" => "DOCC0000000001",
"shipment_value" => 2000,
"shipment_dimensions" => [
"length" => [
"value" => 1
],
"breadth" => [
"value" => 1
],
"height" => [
"value" => 1
],
"weight" => [
"value" => 1
]
],
"return_label_desc_1" => "",
"return_label_desc_2" => "",
"shipment_items" => [
[
"product_id" => "134883075",
"item_content" => (object)null,
"category" => "Apparel",
"product_title" => "Mast&HarbourBlackHandbag => Handbags",
"quantity" => 1,
"cost" => [
"total_sale_value" => 500,
"total_tax_value" => 100
],
"seller_details" => [
"seller_reg_name" => "Seller Registered Legal Name",
"vat_id" => "",
"cst_id" => ""
],
"legal_entity" => "",
"billable_entity" => "",
"cod_remittance_entity" => "",
"item_attributes" => [
[
"name" => "order_id",
"value" => "ABCD"
],
[
"name" => "invoice_id",
"value" => "INV1234"
],
[
"name" => "item_dimensions",
"value" => "l:b:h:w"
],
[
"name" => "brand_name",
"value" => "Peter England"
]
],
"handling_attributes" => [
[
"name" => "isFragile",
"value" => "false"
],
[
"name" => "isDangerous",
"value" => "false"
]
]
]
]
]
]
]
]
]
];

$url = "https://staging.ekartlogistics.com/v2/shipments/create";
$data = json_encode($data);

$headers = array(
"Content-Type: application/json",
"HTTP_X_MERCHANT_CODE: DOC",
);

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "dailyorders:dummyKey"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
curl_setopt($curl, CURLOPT_VERBOSE, true);

$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";

?>


Related Topics



Leave a reply



Submit