1

I have made an rest api.

On previous occasion everything was working fine.

but suddenly now.

Everytime i launch a POST / PUT / DELETE request

I get 301 error in return and that page returns to the GET Request.

in short , it doesn't matter what request I make now everytime GET request is being called. or redirect error to get request occur.

Error log:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.example.in/api/user">here</a>.</p>
</body></html>

Route I used

$v1 = 'api';
$user = 'user/user';

$route['post/(:any)']['get'] = 'post';
$route[$v1 . 'user']['get'] = $v1 . $user;
$route[$v1 . 'user/(:num)/(:any)']['get'] = $v1 . $user . '/type/$1/uid/$2';
$route[$v1 . 'user']['post'] = $v1 . $user;
$route[$v1 . 'user/(:any)']['put'] = $v1 . $user . '/$1';
$route[$v1 . 'user/(:any)']['delete'] = $v1 . $user . '/$1';



EDIT:

I tried to create another dummy api and it too faces the same problem , every request one way or the other directs to get request.

I am pasting its code here.

EDIT 2 Since the issue is resolved I am removing the test url

You can test the api too.

using different HTTP Request GET / POST / PUT / DELETE

<?php
use Restserver\Libraries\REST_Controller;
defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH . 'libraries/REST_Controller.php';
require APPPATH . 'libraries/Format.php';

class Example extends REST_Controller {

    public function users_get(){
        $this->set_response("GET Request", REST_Controller::HTTP_OK); 
    }
    public function users_put(){
        $this->set_response("PUT Request", REST_Controller::HTTP_OK); 
    }
    public function users_post(){
        $this->set_response("POST Request", REST_Controller::HTTP_OK); 
    }
    public function users_delete(){
        $this->set_response("DELETE Request", REST_Controller::HTTP_OK); 
    }   
}
Vicky Salunkhe
  • 9,869
  • 6
  • 42
  • 59

2 Answers2

2

So I resolved the issue.

What I was doing is calling the api using https://example.in

instead I should have called https://www.example.in

that solves the issue being caused by http requests other than GET request.

Vicky Salunkhe
  • 9,869
  • 6
  • 42
  • 59
0

I was with the same error and solve like the answer above.

baseURL in the .env file must contain for production something like

https://www.yourwebsite.com.br/

CodeIgniter 4

Rafael
  • 107
  • 1
  • 5