Home > Mobile >  REST API work on localhost but not on web hosting
REST API work on localhost but not on web hosting

Time:04-23

So this is my first time building a REST API and when I test the API using localhost it's working like a charm but when I host the REST API project on hosting suddenly it does not work. GET and POST functions still working but DELETE and PUT functions aren't.

I am using 000webhost free hosting as my test platform.

my delete function on Controller:

public function delete($id = null)
{
    $model = new BarangModel();
    $data = $model->where('id_barang', $id)->delete($id);
    if ($data) {
        $model->delete($id);
        $response = [
            'status'   => 200,
            'error'    => null,
            'messages' => [
                'success' => 'Barang berhasil dihapus!'
            ]
        ];
        return $this->respondDeleted($response);
    } else {
        return $this->failNotFound('No employee found');
    }
}

My model code:

class BarangModel extends Model
{
    protected $DBGroup              = 'default';
    protected $table                = 'tb_barang';
    protected $primaryKey           = 'id_barang';
    protected $useAutoIncrement     = true;
    protected $insertID             = 0;
    protected $returnType           = 'array';
    protected $useSoftDeletes       = false;
    protected $protectFields        = true;
    protected $allowedFields        = ['kode', 'nama', 'merk', 'tahun', 'jumlah', "ruangan", 'penguasaan', 'keterangan'];

    // Dates
    protected $useTimestamps        = false;
    protected $dateFormat           = 'datetime';
    protected $createdField         = 'created_at';
    protected $updatedField         = 'updated_at';
    protected $deletedField         = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks       = true;
    protected $beforeInsert         = [];
    protected $afterInsert          = [];
    protected $beforeUpdate         = [];
    protected $afterUpdate          = [];
    protected $beforeFind           = [];
    protected $afterFind            = [];
    protected $beforeDelete         = [];
    protected $afterDelete          = [];
}

Error code in console:

Error: Network Error
    at t.exports (createError.js:16:15)
    at XMLHttpRequest.b.onerror (xhr.js:117:14)
ae @ vue.runtime.esm.js:1897
re @ vue.runtime.esm.js:1888
ne @ vue.runtime.esm.js:1848
(anonymous) @ vue.runtime.esm.js:1865
Promise.catch (async)
ie @ vue.runtime.esm.js:1865
n @ vue.runtime.esm.js:2188
ie @ vue.runtime.esm.js:1863
In.t.$emit @ vue.runtime.esm.js:3903
click @ VBtn.ts:163
ie @ vue.runtime.esm.js:1863
n @ vue.runtime.esm.js:2188
Qr.a._wrapper @ vue.runtime.esm.js:6961

xhr.js:210          
DELETE https://xxx.000webhostapp.com/api/index.php/barang/106 net::ERR_HTTP2_PROTOCOL_ERROR
(anonymous) @ xhr.js:210
t.exports @ xhr.js:15
t.exports @ dispatchRequest.js:58
u.request @ Axios.js:108
i.forEach.u.<computed> @ Axios.js:129
(anonymous) @ bind.js:9
(anonymous) @ Barang.vue:292
u @ runtime.js:63
(anonymous) @ runtime.js:294
(anonymous) @ runtime.js:119
i @ asyncToGenerator.js:3
s @ asyncToGenerator.js:25
(anonymous) @ asyncToGenerator.js:32
(anonymous) @ asyncToGenerator.js:21
t @ Barang.vue:291
ie @ vue.runtime.esm.js:1863
n @ vue.runtime.esm.js:2188
ie @ vue.runtime.esm.js:1863
In.t.$emit @ vue.runtime.esm.js:3903
click @ VBtn.ts:163
ie @ vue.runtime.esm.js:1863
n @ vue.runtime.esm.js:2188
Qr.a._wrapper @ vue.runtime.esm.js:6961

On postman, it shows a 'Socket hang up' error.

CodePudding user response:

The PUT and DELETE methods are not available with free hosting on 000webhost. You have to upgrade to the Premium plan to use HTTP methods other than GET and POST See this post.

All methods other than GET and POST are simply terminated with the free hosting plan. This is very amateurish on their part, in my opinion, because they could easily send a response header saying "only available with the premium plan" instead of just flat out terminating the connection.

Meanwhile developers are spending a monolithic waste of time trying to figure out what's wrong with their perfectly good code.

  • Related