2

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

  • Input::all() which gives null
  • Input::json->all() which also gives null
  • Request::getContent() which gives {"lat":56.87987,"lon":97.87678234} but I don't know how to parse that.

Any help would be appreciated.Thanks

1
  • @JayBhatt: "content":null Commented May 5, 2014 at 12:53

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Why was this voted down? It's a working solution, in fact, it's the only answer that actually handles a full json request.
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.