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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大