Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

markcampbell's avatar

Empty HTTP Response

I am using a third-party API to return data via a javascript Fetch call within a web page.

The general setup works and returns the correct JSON I need.

The issue comes when authorisation fails to the third-party API. For this I have a fallback function in the controller that completes the re-authorisation process, which also works fine and it successfully runs my main function again - however this time the response that comes back through Fetch is empty.

Here's an outline of how I have it set up - I've removed some of the code to simplify

// MyController.php

// A function to log in to the API if the initial request fails
public function restLogin($function, $request = null)
{
		//code for API auth stuff...

		if ( $loginResponse->successful() ) {
				// save the latest auth token and then re-run the original API call again...
				//...
				$this->getJobs();
		}
}

public function getJobs()
{
		// code to get tokens and construct api url
		//...

		$jobsResponse = Http::withHeaders([
            'Content-Type' => 'application/json',
            'BhRestToken' => $rest_token,
        ])->get($jobs_url);

		Log::info('get jobs run');

        if ( $jobsResponse->successful() ) {
            $jobsResponseArray = $jobsResponse->json();
			Log::info('get jobs run successful');
			Log::info($jobsResponseArray);
            $response = response()->json([
                'data' => $jobsResponseArray
            ], 200);
            return $response;
        } else {
            $this->restLogin('getJobs');
        }
}

So, what happens is either one of two things:

  1. getJobs() runs successfully the first time since the last saved token was still valid, and it returns me the data I want - so all good
  2. if it fails, it runs the restLogin() function, which works, then it re-runs the getJobs() function, and in my log I can see that $jobsResponse is successful, and also that $jobsResponseArray is complete with the data I need. However when I expect the response for this route overall in Chrome, it is empty.

So it seems for some reason that when auth fails, and it runs through the function a couple of times, the route returns an empty response - even though I can see in the log that the code that should return the right data is being run.

Any idea why this response would end up empty? Is it something in the process of running the same function twice - even though there's only one spot where there's an actual return and it should contain the data that I can see in the log?

Thanks

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It's possible that the response is empty because the restLogin() function is not returning anything. Try modifying the restLogin() function to return the response from getJobs() like this:

public function restLogin($function, $request = null)
{
    //code for API auth stuff...

    if ( $loginResponse->successful() ) {
        // save the latest auth token and then re-run the original API call again...
        //...
        return $this->getJobs(); // return the response from getJobs()
    }
}

Also, make sure to return the response from restLogin() when it's called from getJobs():

public function getJobs()
{
    // code to get tokens and construct api url
    //...

    $jobsResponse = Http::withHeaders([
        'Content-Type' => 'application/json',
        'BhRestToken' => $rest_token,
    ])->get($jobs_url);

    Log::info('get jobs run');

    if ( $jobsResponse->successful() ) {
        $jobsResponseArray = $jobsResponse->json();
        Log::info('get jobs run successful');
        Log::info($jobsResponseArray);
        $response = response()->json([
            'data' => $jobsResponseArray
        ], 200);
        return $response;
    } else {
        return $this->restLogin('getJobs'); // return the response from restLogin()
    }
}

This should ensure that the response from getJobs() is returned all the way up the call stack, even when restLogin() is called.

1 like

Please or to participate in this conversation.