Posting JSON to Laravel

Posting JSON To Laravel

NOTE: this answer is only applicable for old Laravel versions (4.2 and earlier)!

Laravel's Input::all method returns an associative array, not an object of PHP's stdClass.

$data = Input::all();
$data['id']; // The ID of the request

send json to Laravel using postman and android

Wen you want to send data, you will want to use POST or PUT method on your postman, specially if you are sending a body, that means that you are sending data. Get method is used to retrieve data from a service.
Take a look into CRUD functions for more information.
Your postman should look something like this

Last in your android code try to change this line

JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, "http://192.168.1.4:8000/r",requestJsonObject , new Response.Listener<JSONObject>() {

to use Request.Method.POST

Sending JSON to Laravel using POST request

The element Title of your request is empty or is not present in the request

This can be due to a typo, error in the caps or something similar.

The first error shows an error in thee database trying to insert a record with an explicit required filed with null.

The second tells you are trying to access to a property that has not being initialized.

Edit:

After reviewed the dump of your variable and your request it's visible that you are setting your requests as an element of one array.

That is why the member Title doesn't exists.
If you try to access like this:
$testData[0]->Title you will find your value.

You don't get errors before because you are assigning the values to the main $testData object.

My suggestion is update your request like this:

{
"UserTodoId": 0,
"UserDetailId": 1,
"Title": "Test item2",
"Details": "This is a test item2",
"completed": 1,
"modified": 0,
"DateCreated": "2020-05-20T00:00:00.000000Z",
"DateModified": "2020-05-22T00:00:00.000000Z"
}

Sending just one object instead of an array with one element.

How to receive JSON POST request in Laravel

First of all you need to ignore verifying csrf field for that route.

In your app/Http/Middleware/VerifyCsrfToken.php

protected $except = [
'/react_test',
];

Secondly use argument as Request in your index method.

use Illuminate\Http\Request;

public function index(Request $request){

$n = $request->input('name');

DB::table('admin_panels')->insert(['discord' => $n, 'twitter' => 'anan']);

return response('helal');

}

How to get and send Json strings in Laravel?

Thank you everyone I found Answer

web.php like that

Route::post('test3', 'UssdController@test3');

UssdController.php like that

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
class UssdController extends Controller
{
public function test3(Request $request){

$data=array(
"message" =>'We Display This in Phone Screen',
"applicationId"=>$request->applicationId,
"password"=>"password",
"version"=>"1.0",
"sessionId"=>$request->sessionId,
"ussdOperation"=>"mt-cont",
"destinationAddress"=>$request->sourceAddress,
"encoding"=>"440",
"chargingAmount"=>"5",
);

$jsonData =json_encode($data);// response()->json($data);
$json_url = "http://localhost:7000/ussd/send";

$ch = curl_init( $json_url );
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json') ,
CURLOPT_POSTFIELDS => $jsonData
);
curl_setopt_array( $ch, $options );
$result = curl_exec($ch);
Log::info($result);
curl_close($ch);
}
}

Laravel API not accepting JSON request from Postman

Comments are not permitted in JSON.
There's a bug in the field Body -> raw -> json



Related Topics



Leave a reply



Submit