How should I format a JSON POST request to my rails app?

前端 未结 2 1916
余生分开走
余生分开走 2021-01-30 03:17

My Rails app has a player class that works perfectly. Players can be created, deleted, and updated from my rails control panel without any issues.

I would like a remote

相关标签:
2条回答
  • 2021-01-30 03:48

    First of all, I think your data format is ok and is not the problem here. When I ran exactly into the same problem it was because I did not send the Content-Type: application/json header along with my request.

    In Postman, you can select the 'raw' data format and then 'JSON (application/json)' to include this header. In my case it looks like this:

    Screenshot of sending a JSON request to a rails application in Postman

    Alternatively, you can also try it with curl (source):

    curl -X POST -H "Content-Type: application/json" -d '{"player": {"name": "test","room_id": 0,"skin_id": 1,"head_id": 2,"torso_id": 3,"legs_id": 4,"x_position": 5,"y_position": 6,"direction": 7,"action": "","gender": "f"}}' localhost:3000/players.json

    If you omit the -H "Content-Type: application/json", then you will receive a 400 response with the "param not found" error, if you include it it should work.

    0 讨论(0)
  • 2021-01-30 03:56

    If you are trying this:

    via Postman - Under form-data tab, you need to have the vars as :

    player[name]
    player[room_id]
    .
    .
    

    via jQuery:

    $.ajax({ url: 'url', type: 'post', data: { player: { name: 'Test', room_id: '0' } } })
    
    0 讨论(0)
提交回复
热议问题