Welcome Guest, Not a member yet? Register   Sign In
Header may not contain more than a single header, new line detected
#1

I am getting the error below
"Header may not contain more than a single header, new line detected"
The code sending the request
PHP Code:
try{
                $client = \Config\Services::curlrequest();
                $this->response $client->request('POST'$this->base_url.'api/buy/data', [
                    'json' => [
                        'api_key' => $this->api_key,
                        'access_key' => $this->access_key,
                        'data' => $request_body
                    
],
                    'timeout' => 30,
                    'verify' => false
                
]);

                $response_body json_encode($this->response->getBody(), true);
                $status_code $this->response->getStatusCode(); //201

                if($status_code == "201" && $response_body['status'] == "success"){
                    $data = [
                        'status'=>'success',
                        'message'=>'Order created'
                    ];

                    return $this->respondCreated($data);
                }
                else{
                    $data = [
                        'status'=>'error',
                        'message'=>'Order created'
                    ];

                    return $this->respond($data200);
                }



            }
            catch (\Exception $e){
                $data = [
                    'status'=>'error',
                    'message'=>$e->getMessage()
                ];
                return $this->respond($data400);
            

The error is from an API request sent.
Code:
{
    "title": "ErrorException",
    "type": "ErrorException",
    "code": 500,
    "message": "Header may not contain more than a single header, new line detected",
    "file": "C:\\laragon\\www\\shazampay_api\\vendor\\codeigniter4\\framework\\system\\HTTP\\ResponseTrait.php",
    "line": 472,
    "trace": [
        {
            "function": "errorHandler",
            "class": "CodeIgniter\\Debug\\Exceptions",
            "type": "->",
            "args": [
                2,
                "Header may not contain more than a single header, new line detected",
                "C:\\laragon\\www\\shazampay_api\\vendor\\codeigniter4\\framework\\system\\HTTP\\ResponseTrait.php",
                472,
                {
                    "name": "Cache-control"
                }
            ]
        },
        {
            "file": "C:\\laragon\\www\\shazampay_api\\vendor\\codeigniter4\\framework\\system\\HTTP\\ResponseTrait.php",
            "line": 472,
            "function": "header",
            "args": [
                "Cache-control: no-store, max-age=0, no-cache,  no-store, no-cache, must-revalidate\r,  no-store, max-age=0, no-cache\r",
                false,
                400
            ]
        },
        {
            "file": "C:\\laragon\\www\\shazampay_api\\vendor\\codeigniter4\\framework\\system\\HTTP\\ResponseTrait.php",
            "line": 442,
            "function": "sendHeaders",
            "class": "CodeIgniter\\HTTP\\Response",
            "type": "->",
            "args": []
        },
        {
            "file": "C:\\laragon\\www\\shazampay_api\\vendor\\codeigniter4\\framework\\system\\CodeIgniter.php",
            "line": 988,
            "function": "send",
            "class": "CodeIgniter\\HTTP\\Response",
            "type": "->",
            "args": []
        },
        {
            "file": "C:\\laragon\\www\\shazampay_api\\vendor\\codeigniter4\\framework\\system\\CodeIgniter.php",
            "line": 451,
            "function": "sendResponse",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->",
            "args": []
        },
        {
            "file": "C:\\laragon\\www\\shazampay_api\\vendor\\codeigniter4\\framework\\system\\CodeIgniter.php",
            "line": 320,
            "function": "handleRequest",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->",
            "args": [
                null,
                {
                    "handler": "file",
                    "backupHandler": "dummy",
                    "storePath": "C:\\laragon\\www\\shazampay_api\\writable\\cache/",
                    "cacheQueryString": false,
                    "prefix": "",
                    "ttl": 60,
                    "reservedCharacters": "{}()/\\@:",
                    "file": {
                        "storePath": "C:\\laragon\\www\\shazampay_api\\writable\\cache/",
                        "mode": 416
                    },
                    "memcached": {
                        "host": "127.0.0.1",
                        "port": 11211,
                        "weight": 1,
                        "raw": false
                    },
                    "redis": {
                        "host": "127.0.0.1",
                        "password": null,
                        "port": 6379,
                        "timeout": 0,
                        "database": 0
                    },
                    "validHandlers": {
                        "dummy": "CodeIgniter\\Cache\\Handlers\\DummyHandler",
                        "file": "CodeIgniter\\Cache\\Handlers\\FileHandler",
                        "memcached": "CodeIgniter\\Cache\\Handlers\\MemcachedHandler",
                        "predis": "CodeIgniter\\Cache\\Handlers\\PredisHandler",
                        "redis": "CodeIgniter\\Cache\\Handlers\\RedisHandler",
                        "wincache": "CodeIgniter\\Cache\\Handlers\\WincacheHandler"
                    }
                },
                false
            ]
        },
        {
            "file": "C:\\laragon\\www\\shazampay_api\\public\\index.php",
            "line": 37,
            "function": "run",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->",
            "args": []
        }
    ]
}

My CI version is 4.1.9 
PHP Version 7.4


try{
Reply
#2

Are you setting your headers?
Reply
#3

(This post was last modified: 03-18-2022, 01:07 PM by vimkaf.)

(03-17-2022, 01:46 AM)iRedds Wrote: Are you setting your headers?

I have fixed this issue, the problem was the Cache-Control headers were getting repeated which was adding \r to each repitition.

So, what I did was to set the response headers manually. 

Which solved the problem.
Reply
#4

Do not reassign $this->response in the controller.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB