Skip to content

Instantly share code, notes, and snippets.

@EvertonSilva
Last active February 11, 2018 20:05
Show Gist options
  • Save EvertonSilva/20e9295359a95cfd80f43d09031bcdaa to your computer and use it in GitHub Desktop.
Save EvertonSilva/20e9295359a95cfd80f43d09031bcdaa to your computer and use it in GitHub Desktop.
Send curl POST to rails app

Make a POST request to Rails App using curl

1. Make a GET request to get a csrf token

curl http://exemplo.com.br --cookie-jar cookie | grep csrf

The --cookie-jar option creates a cookie in a file called "cookie" xD. This cookie is needed to validate the POST request

The command above will output something like this

<meta name="csrf-token" content="rfk1uPeJdFwIpyOFpyfCX9GMVA/RSmZRWsje52OA4FtDjVG8eCoppYvn2OIyS8sIUesAmOBJbd2ey0B7zay9yA==" />

Copy the content of meta tag csrf-token

2. Send the data to a create method in a Rails controller

curl http://exemplo.com.br/controller_name/post_action --cookie-jar cookie --cookie cookie\
--data-urlencode "authenticity_token=rfk1uPeJdFwIpyOFpyfCX9GMVA/RSmZRWsje52OA4FtDjVG8eCoppYvn2OIyS8sIUesAmOBJbd2ey0B7zay9yA=="\
--data "modelName[attribute]=Value of the attribute to store"
@majioa
Copy link

majioa commented Feb 11, 2018

curl -X POST -H 'Content-Type:application/json' -d 'authenticity_token=Gd/nHALaKAAKI7gPzoCevjaxCZ2ODRGVsK35Smsv+p648aozyQ/qEYNQX4c8ZJBKv5SUOlNQATUte38RvH7KbQ==' -d '
{
   "phone": "+77777777777"
}
' http://localhost:23456/api/v1/lp/phones/assign

And log rails:

ActionDispatch::Http::Parameters::ParseError (822: unexpected token at 'authenticity_token=Gd/nHALaKAAKI7gPzoCevjaxCZ2ODRGVsK35Smsv+p648aozyQ/qEYNQX4c8ZJBKv5SUOlNQATUte38RvH7KbQ==&
{
   "phone": "+77777777777"
}
'):

That is strangely (((

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment