Rajat Saxena
Rajat Saxena

Reputation: 3925

How to get input data from PUT request in a rest api built using Laravel 4

I am using the following request to post data to the REST api using PUT method:

CURL.EXE -i -H "Content-Type: application/json" -X put -d '{"lat":56.87987,"lon":97.87678234}' http://webservice.dev:8081/api/v1/interface/123

But I don't know how to get the lat/lon data into the Restful Controller.I have tried

Any help would be appreciated.Thanks

Upvotes: 2

Views: 5594

Answers (3)

ollieread
ollieread

Reputation: 6301

Laravel is funny when posting JSON to it. It's a bit cheaty as it expects that the JSON data is part of a POST body query string, rather than the entire body is json.

Instead of Input::json->all(), use Request::json()->all().

I actually recently built an API that accepts pure JSON requests for GET, POST, DELETE, PUT and PATCH.

Example:

private function parseRequest() {
    $query = Request::query();

    if(!empty($query['includes'])) {
        $this->includes = explode(',', $query['includes']);
        unset($query['includes']);
    }

    if(!empty($query['page']) && $query['page'] > 0) {
        $this->page = $query['page'];
        unset($query['page']);
    }

    if(!empty($query['count']) && $query['count'] > 0) {
        $this->count = $query['count'];
        unset($query['count']);
    }

    if(!empty($query['token'])) {
        $this->token = $query['token'];
        unset($query['token']);
    }

    $this->query = $query;

    $this->parameters = Request::json()->all();

    $this->route = Route::current();
}

Hope that helps.

Upvotes: 3

Rajat Saxena
Rajat Saxena

Reputation: 3925

Okay.I am now using this work around

CURL.EXE -d 'lat=56.699857676' -X post "http://webservice.dev:8081/api/v1/interface/pF265UO3d68UNp0ID6hwclL88f7F5pz?_method=PUT&lat=56.9837487943&lon=78.8974982374"

I don't know whether this method is the right way of doing PUT request or is it safe or not.

Upvotes: 1

Vit Kos
Vit Kos

Reputation: 5755

Had the same issue, solved it with placing this under my filters.php file

App::before(function($request)
{
    if (!App::runningInConsole())
    {
      header('Access-Control-Allow-Origin', '*');
      header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
      header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Client, Authorization');

      exit;
  }
});

After this you can get all the params via Input

Upvotes: 0

Related Questions