fmg
fmg

Reputation: 3

Retrieving JSON content

i am a beginner on laravel. i have problem on creating my POST API. i want to use data retrieved from json POST from other application. but it always return null so i did some investigating on the request.

when i return dd($request), the result is

Illuminate\Http\Request {#43 ▼
  #json: Symfony\Component\HttpFoundation\ParameterBag {#35 ▶}
  #convertedFiles: []
  #userResolver: Closure($guard = null) {#1241 ▼
    class: "Illuminate\Auth\AuthServiceProvider"
    this: Illuminate\Auth\AuthServiceProvider {#22 …}
    use: {▶}
    file: "C:\xampp\htdocs\attendance_online\vendor\laravel\framework\src\Illuminate\Auth\AuthServiceProvider.php"
    line: "105 to 107"
  }
  #routeResolver: Closure() {#1246 ▼
    class: "Illuminate\Routing\Router"
    this: Illuminate\Routing\Router {#26 …}
    use: {▶}
    file: "C:\xampp\htdocs\attendance_online\vendor\laravel\framework\src\Illuminate\Routing\Router.php"
    line: "655 to 657"
  }
  +attributes: Symfony\Component\HttpFoundation\ParameterBag {#45 ▼
    #parameters: []
  }
  +request: Symfony\Component\HttpFoundation\ParameterBag {#35 ▼
    #parameters: []
  }
  +query: Symfony\Component\HttpFoundation\InputBag {#51 ▼
    #parameters: []
  }
  +server: Symfony\Component\HttpFoundation\ServerBag {#47 ▼
    #parameters: array:23 [▶]
  }
  +files: Symfony\Component\HttpFoundation\FileBag {#48 ▼
    #parameters: []
  }
  +cookies: Symfony\Component\HttpFoundation\InputBag {#46 ▼
    #parameters: []
  }
  +headers: Symfony\Component\HttpFoundation\HeaderBag {#49 ▼
    #headers: array:6 [▼
      "accept-encoding" => array:1 [▼
        0 => "gzip,deflate"
      ]
      "content-type" => array:1 [▼
        0 => "application/json"
      ]
      "content-length" => array:1 [▼
        0 => "231"
      ]
      "host" => array:1 [▼
        0 => "localhost:8000"
      ]
      "connection" => array:1 [▼
        0 => "Keep-Alive"
      ]
      "user-agent" => array:1 [▼
        0 => "Apache-HttpClient/4.5.5 (Java/12.0.1)"
      ]
    ]
    #cacheControl: []
  }
  #content: """
    {\n
    \t"id_employee" = "4",\n
    \t"status" = "1",\n
    \t"latitude" = "3.4141414",\n
    \t"longitude" = "98.444444",\n
    \t"timestamp" = "2020-10-11 09:58:00.0000000",\n
    \t"approval" = "1",\n
    \t"message" = "this is message",\n
    \t"picture_reference" = "this is pic"\n
    }
    """
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/api/Attendance"
  #requestUri: "/api/Attendance"
  #baseUrl: ""
  #basePath: null
  #method: "POST"
  #format: null
  #session: null
  #locale: null
  #defaultLocale: "en"
  -preferredFormat: null
  -isHostValid: true
  -isForwardedValid: true
  -isSafeContentPreferred: null
  basePath: ""
  format: "html"
}
}

when i return $request->getcontent() the result is

{ "id_employee" = "4", "status" = "1", "latitude" = "3.4141414", "longitude" = "98.444444", "timestamp" = "2020-10-11 09:58:00.0000000", "approval" = "1", "message" = "this is message", "picture_reference" = "this is pic" }

but when i return $request->all() is says

<Empty JSON content>

whenever i use $request->id_employee or any other data, it always return null. when i put parameter on the URL like "?id_employee=1", $request->id_employee will return 1. i do not want to put the data on the URL, so how do i get all the content on $request?

edit: i already tried

$id_employee = $request->id_employee;
$id_employee = $request->input("id_employee")
$id_employee = $request->input("content.id_employee")

and already confirm that $request->isjson() return 1

Upvotes: 0

Views: 1162

Answers (1)

Lorna Mitchell
Lorna Mitchell

Reputation: 1996

What version of Laravel do you have? You should be able to decode the JSON content, like this:

$content = $request->getContent()
$data = json_decode($content, true)
dd(data)

Check that the JSON is valid though - the example that you posted in your original questions had = where the : should normally go! The JSON needs to be valid and if the request has the correct Content-Type header then your call to request->input("employee_id") should work. That it doesn't makes me wonder if the JSON is well-formatted so definitely check that as well.

Upvotes: 1

Related Questions