Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4110

API returning text/html while it should return JSON

$
0
0

I am new to web service development. I didn't use any framework. I did it by scratch. I wanted to create an RESTFUL API service which I want to use for my iOS application. But I am getting some error related to content-type

This is my api.php script:

<?php    require_once "scripts/Database.php";    require_once "scripts/Database_Handler.php";    require_once "scripts/config.php";    header('Content-type:application/json;charset=utf-8'); //set the content type    $db_obj = new Database();     $db = $db_obj->get_database_connection(); //returns the database connection object    $handler = new Database_Handler($db); //this is the class that does all the queries to database    $method = $_SERVER['REQUEST_METHOD']; //method is POST here    $url =  explode("/", $_SERVER['PATH_INFO']);    $request = end($url);    if($request == 'all_products'){        $result_array = $handler->get_all_products();    }    else if ($request == 'customers'){        $result_array = $handler->get_customers();    }    else{        $result_array = $handler->get_all_products_available_in_stores();    }    echo json_encode($result_array);?>

When I try to consume the resource with the help of AFNetworking in iOS, it gives me following error.

"Request failed: unacceptable content-type: text/html" UserInfo={AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7fe6b241c0b0> { URL: http://........../....../api.php/customers } { status code: 200, headers {    Connection = "keep-alive";"Content-Encoding" = gzip;"Content-Type" = "text/html";    Date = "Tue, 29 Dec 2015 11:16:01 GMT";    Server = "nginx/1.8.0";"Transfer-Encoding" = Identity;} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, NSErrorFailingURLKey=http://........../....../api.php/customers}

Can anyone please help me to solve the problem from the API end?


Viewing all articles
Browse latest Browse all 4110

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>