i am testing an external API
first i am getting the token and all data from the API as showing below:
Controller:
public function index()
{
$response = Http::post('http://example.com/authenticate', [
'Username' => 'ADMIN',
'Password' => 'ADMIN',
'Token' => 'FK98DL-...',
]);
$token = json_decode($response, true);
$apiURL = 'http://example.com/api/Order/';
$headers = [
'Content-Type' => 'application/json',
'Authorization' => $token,
];
$response2 = Http::withHeaders($headers)->get($apiURL);
$data = $response2->json();
return view('api.auth.orders.index',compact ('data'));
}
and i am able to get the token and data from the above function
now in the next function to show a particular order:
public function show($DocNo)
{
$response = Http::post('http://example.com/authenticate', [
'Username' => 'ADMIN',
'Password' => 'ADMIN',
'Token' => 'FK98DL-...',
]);
$token = json_decode($response, true);
$response3 = Http::withHeaders(['Authorization' => $token,'Content-Type' => 'application/x-www-form-urlencoded'])->get('http://example.com/api/Order/GetOrder/', [
'DocNo' => $DocNo,
])->json();
return $response3;
from the above function i am getting this error:
Object reference not set to an instance of an object.
i tested the "Show" function with Postman and i am able to get the response
api.php:
Route::get('show/{id}', 'show')->name('show');
anchor tag that will redirect to particular order in the view:
<a href="{{ route('orders.show', $item['DocNo'])}}">EDIT</a></td>
what seems to be the problem here?
CodePudding user response:
You will need to use asForm
method of Http client like:
$response3 = Http::asForm()->withHeaders(['Authorization' => $token])
->get('http://example.com/api/Order/GetOrder/', [
'DocNo' => $DocNo,
]);
CodePudding user response:
SOLUTION:
I used Guzzle and Psr7:
$client = new Client();
$headers = [
'Authorization' => $token,
'Content-Type' => 'application/x-www-form-urlencoded'
];
$options = [
'form_params' => [
'DocNo' => $DocNo
]
];
$request = new Psr7Request('GET', 'http://example.com/api/Order/GetOrder/', $headers);
$res = $client->sendAsync($request, $options)->wait();
$data = $res->getBody();
echo $data;