duanjian3920 2015-10-28 02:12
浏览 235

Laravel 5 - 使用curl发布JSON数据

I'm trying to POST data using command line using curl.

curl -v POST -d ' { "data1": "sample1", "data2": "sample2" } ' -H "Content-Type: application/json" -H "Authorization: BASIC vkslnkg561mZEqCq3l3RglAOAZ7d8XBeg2VjIAyC" http://localhost/Bowling/public/api/foo

When I try to POST from POSTMAN, I get the data back. But when I use curl, I'm getting a 500 Internal Server Error.

My routes.php

Route::any('api/foo', function () { 
    return json_encode("Some Data"); 
});

May I know is it something wrong with my curl statement or some authentication problem that I need to fix in Laravel.

I even tried with

curl -X POST -H "Content-Type: application/json" -d '{"key":"val"}' -H "Authorization: BASIC 0xD8BC2CLksEMh1ScRdG8dWanCDazYiQsGL7sYnl" http://localhost/Bowling/public/api/foo
  • 写回答

1条回答 默认 最新

  • douzhongju8780 2015-10-28 05:38
    关注

    There is nothing wrong in what you are doing. Its just the delay in your calls which is causing the token mismatch. I faced the same issue while manually testing my APIs via postman. Sad that Laravel doesn't have any decorator to exempt CSRF like Django.

    On Postman, I was able to test my APIs issues by writing a route that returns a csrf token. I then copy that token and pass it as form-data in my next POST call. I couldn't find any other alternative.

    Let me know if it works for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置