drcmg28484 2016-05-01 11:57
浏览 38
已采纳

从网站上消费API

I'm developing a website and REST API which are a part of the same project folder.

I've developed a REST api as below

URL: example.com/login
Type: POST
Request Body:
{
    "credentials": {
        "username":"dd",
        "password":"dd"
    },
    "appInfo": {
        "version": "1.0"
    }
}

In the login method (UserController) I'm fetching the request body as below

json_decode(Request::instance()->getContent());

Now I'm developing a webpage and want to invoke this API from there. I'm using the below code (HomeController)

public function acceptlogin(Request $request) {
    $data = array(
        'credentials'=> array(
            'username'=>$request->email,
            'password'=>$request->password
        ),
        'appInfo'=> array(
            'version'=> "1.0"
        )
    );
    $request1 = Request::create('example.com/login', 'POST', $data);
    $response = Route::dispatch($request1);
}

I'm able to invoke the login api from the above code but the below code in the login method returns empty.

json_decode(Request::instance()->getContent());

Can you please suggest on how to invoke a local API (within the same project) in Laravel

  • 写回答

1条回答 默认 最新

  • douchan0523 2016-06-07 10:32
    关注

    The problem is fixed with a workaround. I'm using the below code to invoke LoginController

    (new UserController)->loginNew($data)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan