5531

Sending data with put / post to a webservice in Laravel

Question:

I'm trying to create a dynamic method to send / receive my data with my webservice using guzzle and laravel

I have the following structure in my controller

class TipoProjetoController extends Controller { private $Tabela = 'tipoprojeto'; public function AppWebService($Who, $Type, $Data) { $Client = new Client(['base_uri' => config('constants.caminhoWS').$Who]); $response = $Client->request($Type, $Data); return $response->getBody()->getContents(); } public function index() { $Dados = $this->AppWebService($this->Tabela,'GET',''); $Titulo = 'Tipos de Projeto'; $jsonObj = json_decode($Dados); $Obj = $jsonObj->data; return view('Painel.TipoProjeto.index',compact('Obj','Titulo') ); } public function create() { return view('Painel.TipoProjeto.create-edit'); } public function store(Request $request) { // } public function show($id) { // } public function edit($id) { $Dados = $this->AppWebService($this->Tabela.'/'.$id,'GET',''); if( $Dados == null ) return redirect()->route('TipoProjeto.index'); else { $Objeto = json_decode($Dados); return view('Painel.TipoProjeto.create-edit',compact('Objeto')); } } public function update(Request $request, $id) { $Dados = $this->AppWebService($this->Tabela.'/'.$id, 'PUT', ''); dd($Dados); } public function destroy($id) { // } }

Until then everything worked perfectly in my Index and Edit method, because the method of sending is GET. now I am trying to send a request to the server with the PUT method and I am not getting.

The AppWebService method has as the first parameter the name of the route that I am accessing, the type and the information that I am going to pass.

I tried this way

public function update(Request $request, $id) { $Dados = $this->AppWebService($this->Tabela.'/'.$id, 'PUT', ['data'=>'$request']); dd($Dados); }

and the error was

<blockquote>

(1/1) InvalidArgumentException URI must be a string or UriInterface

</blockquote>

my request received from my client was

dd($request) Request {#38 ▼ #json: null #convertedFiles: null #userResolver: Closure {#154 ▶} #routeResolver: Closure {#156 ▶} +attributes: ParameterBag {#40 ▶} +request: ParameterBag {#39 ▶} +query: ParameterBag {#46 ▶} +server: ServerBag {#42 ▶} +files: FileBag {#43 ▶} +cookies: ParameterBag {#41 ▶} +headers: HeaderBag {#44 ▶} #content: null #languages: null #charsets: null #encodings: null #acceptableContentTypes: null #pathInfo: "/Painel/TipoProjeto/1" #requestUri: "/index.php/Painel/TipoProjeto/1" #baseUrl: "/index.php" #basePath: null #method: "PUT" #format: null #session: Store {#185 ▶} #locale: null #defaultLocale: "en" -isHostValid: true -isForwardedValid: true basePath: "" format: "html" }

as my application is in laravel and my webservice too, is it any problem that I send the request this way? or do I have to extract and send it? and how do I send the put and post methods to my dynamic function?

[EDIT] File Web Service, Route and Controller

Route::group(['prefix' => 'api'],function(){ Route::group(['prefix' => 'user'], function(){ Route::group(['prefix' => 'tipoprojeto'], function(){ Route::get('/','Painel\TipoProjetoController@All'); Route::get('{id}','Painel\TipoProjetoController@Get'); Route::post('','Painel\TipoProjetoController@Save'); Route::put('{id}','Painel\TipoProjetoController@Update'); Route::delete('{id}','Painel\TipoProjetoController@Delete'); }); }); });

Webservice method that receives my request

class TipoProjetoController extends Controller { public function Update(Request $request, $id){ return 'Change data of id:'.$id; } }

Answer1:

Try with ['data'=>$request] instead of ['data'=>'$request']

you're also using $Client->request(); wrong. The second parameter should be the URI, not an array, hence your error.

Also the base URI is not meant to be used that way, so instead try

$Client = new Client(); $url = config('constants.caminhoWS').$Who; $response = $Client->request($Type,$url, $Data);

(if you are going to use a base URI, use the second parameter in $Client->request() to pass the rest of the URL, like

$Client = new Client(['base_uri' => config('constants.caminhoWS')]); $response = $Client->request($Type, $Who, $Data);

Then Guzzle will construct the URL... <a href="http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request" rel="nofollow">http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request</a>

Recommend

  • Wordpress Geocoding using direction or a map
  • A simple datepicker in VueJS
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • How to parsing NSDate to RFC 822 always use in English?
  • iOS Date formatting
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Ionic storage “get” returns null only on the second call within method
  • Laravel 4 routing not working due to .htaccess file?
  • Iron Router: How do I send data to the layout?
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • pip in virtualenv gets ConnectTimeoutError
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Element.tagName for python not working
  • How can I speed up CURL tasks?
  • Bad request using file_get_contents for PUT request in PHP
  • Java: can you cast Class into a specific interface?
  • Seeking advice on Jetty HttpClient Hang
  • AES padding and writing the ciphertext to a disk file
  • How would I use PHP exceptions to define a redirect?
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Sending data from AppleScript to FileMaker records
  • Symfony2: How to get request parameter
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • log4net write single file for each call to log.info
  • Load html files in TinyMce
  • Getting error when using KSoap library to consume .NET web services
  • Understanding cpu registers
  • How to stop GridView from loading again when I press back button?
  • need help with bizarre java.net.HttpURLConnection behavior
  • LevelDB C iterator
  • Add sale price programmatically to product variations
  • How can i traverse a binary tree from right to left in java?