Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9011827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:50:35+00:00 2026-06-16T02:50:35+00:00

I am writing a REST API and currently testing some things. I am trying

  • 0

I am writing a REST API and currently testing some things. I am trying to make it send an error response when it does not find anything in the database.

The part that is running (because i am testing currently by just entering the url into my browser) is below:

    else if ($request->getHttpAccept() === 'xml')  
    {  
        if(isset($data['s']) && isset($data['n'])) {
            $id = $db->getAlcoholIDByNameSize($data['n'], $data['s']);
            $prices = $db->pricesByAlcohol($id);
        }
        if(isset($id)) {
            $resData = array();
            if(!empty($prices)) {
                foreach($prices as $p) {
                    $store = $db->store($p['store']);
                    array_push($resData, array('storeID' => $p['store'], 'store_name' => $store['name'], 'store_gps' => $store['gps'], 'price' => round($p['price'], 2)));
                }
                RestUtils::sendResponse(200, json_encode($resData), 'application/json'); 
            } else {
                RestUtils::sendResponse(204, 'error', 'application/json'); 
            }
        } else {
            RestUtils::sendResponse(204, 'error', 'application/json'); 
        }
        //RestUtils::sendResponse(501, "xml response not implemented", 'application/xml');  
    }  

everything works fine if the queries return something to be stored in $id and $prices. If they do not exist in the database, however, it tries to load the page, and then goes back to the previous page you were on. You can see the behavior by going to:

http://easyuniv.com/API/alc/coorsa/2   <-- works
http://easyuniv.com/API/alc/coors/3    <-- works
http://easyuniv.com/API/alc/coorsa/5   <-- doesn't work(or anything else, the two above are the only ones)

here is my sendResponse function:

   public static function sendResponse($status = 200, $body = '', $content_type = 'text/html')  
    {  
        $status_header = 'HTTP/1.1 ' . $status . ' ' . RestUtils::getStatusCodeMessage($status);  
        // set the status  
        header($status_header);  
        // set the content type  
        header('Content-type: ' . $content_type);  

        // pages with body are easy  
        if($body !== '')  
        {  
            $temp = json_decode($body);
            $body = json_encode(array('result' => array('status' => $status, 'message' => RestUtils::getStatusCodeMessage($status)), 'data' => $temp));
            // send the body  
            echo $body;  
            exit;  
        }  
        // we need to create the body if none is passed  
        else  
        {           
            $body = "else".json_encode(array('result' => array('status' => $status, 'message' => RestUtils::getStatusCodeMessage($status))));

            echo $body;  
            exit;  
        }  
    } 

I have tried debugging using echos but I cant seem to narrow down what the issue is. Any help would be appreciated, thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:50:37+00:00Added an answer on June 16, 2026 at 2:50 am

    The problem is that when there is no appropriate data found in the data base you are returning HTTP 204 which is telling the browser there is absolutely nothing for it to display. This is not true in your case.

    You still want to output the message that there was nothing found.

    To fix you need to replace the two instances of 204 in your code with 200.

    I modified tested your code using: Note, nothing will display as is. To get the message to display change 204 to 200 in the $status_header variable.

    <?php
            $status_header = 'HTTP/1.1 204';
    
            // set the status  
            header($status_header);  
            // set the content type  
            header('Content-type: text/html');
    
            echo "Can you see me???";
    ?>
    

    Note: When testing this always close the tab and use a fresh tab for each call or else it will look like it is loading data from the previous call, like you have explained.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a REST API in PHP and I can't find any resources explaining
I am trying to create a REST api using Spring MVC. Instead of writing
I'm writing a REST api. It returns a header status 400 on error. However,
I am writing some code against the Azure Table Storage REST API. The API
I'm currently developing a C# MVC REST web api, and am trying to choose
I'm writing a app that exposes a REST API. Some of the query parameters
I could do with some help on my REST API. I'm writing a Node.js
I am currently writing a rest API in python with the microframework Flask. It's
I am currently writing a REST API using Symfony2. I started using Symfony2 one
I am writing some rest calls to the LinkedIn API using web request and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.