itstahir's avatar

Get request Instead of Post request

i am using http in angular and send post request to the lumen backend but its redirect as a get Post request from http is working i case i send post request to other server but for my lumen db it redirect to get hear is my http call


postCountsData(data: any) {
    let options = this.createRequestOptions();
  
    return  this.http.post(this.serverUrl+'counts', {duroodCount: 11}, {headers: options});
  }

hear is the url private serverUrl = "http://localhost:8000/api";

these are my request header

private createRequestOptions() {
    let headers = new HttpHeaders({
      "Content-Type": "application/x-www-form-urlencoded"
    });
    return headers;
  }
// routes of lumen
$router->group(['prefix' => 'api'], function () use ($router) {
  $router->get('counts',  ['uses' => 'CountsController@showAllCounts']);

  $router->post('counts', ['uses' => 'CountsController@create']);
  
  $router->get('counts/{id}', ['uses' => 'CountsController@showOneCount']);

  $router->put('counts/{id}', ['uses' => 'CountsController@update']);
  
  $router->delete('counts/{id}', ['uses' => 'CountsController@delete']);
});

this is the create method of controller

 public function create(Request $request)
    {
        $Counts = Counts::create($request->all());

        return response()->json($Counts, 201);
    }

this is the output

Request URL:http://localhost:8000/api/counts
Request Method:GET
Status Code:201 created
0 likes
6 replies
D9705996's avatar

I'm not sure if you have a missing forward slash as

serverUrl = "http://localhost:8000/api";
...
this.serverUrl+'count' // gives you http://localhost:8000/apicounts";

Maybe a typo. If not I'm not sure from description what your exact issue is.

itstahir's avatar

i have almost find the issue but it is unclear yet , wait i will describe you about it when i request the data from http as a post request its in json formate and the header are www urlencoded so lumen does not received the params

if i force http to send data as www urlencoded like

  let body = new URLSearchParams();
    body.set('duroodCount', "tahir");
    body.set('party_id', "23");

force to send data in key value pair then the data is successfully post in the db but the strange this is that my data is saved in the database and the network in that case is the same Get request also in network their are no params its strange but my data is posted in the mysql db

D9705996's avatar

You showed the code for your postCountsData function but you dont show where/how you call this. It might help identify the problem

itstahir's avatar

hear i am calling this function as this function is in the service so i just subscribe it .

 onButtonTap() {
    this.userService.postCountsData([]).subscribe(res => {
      console.dir(res);
      // this.router.back()
    }, (error) => {
      console.dir(error);
    });
  }

calling the function is not the issue i think the problem is with headers and data i am passing they are not the same as laravel lumen requires i think

D9705996's avatar

So is everything essentially working but you are confused by this

Request URL:http://localhost:8000/api/counts
Request Method:GET
Status Code:201 created

Where are you seeing this data? Is from your console.dir(res); in dev tools? From what your have shown so far I cannot see you doing any get http request just a post. The data you have shown doesn't match the axios response format

{
  // `data` is the response that was provided by the server
  data: {},

  // `status` is the HTTP status code from the server response
  status: 200,

  // `statusText` is the HTTP status message from the server response
  statusText: 'OK',

  // `headers` the headers that the server responded with
  // All header names are lower cased
  headers: {},

  // `config` is the config that was provided to `axios` for the request
  config: {},

  // `request` is the request that generated this response
  // It is the last ClientRequest instance in node.js (in redirects)
  // and an XMLHttpRequest instance the browser
  request: {}
}

So I'm a little bit stumped/confused

Please or to participate in this conversation.