Rails / Redmine - Unable to validate authentication token results in error 422

I am working on a plugin for the Redmine platform and I would like to attach the file to the document (basically upload the file) using a link instead of a form to do this. I create POST requests inside a method.

I followed the instructions here , I set the content type application/octet-streamaccording to the request, after which I placed the contents of the file in the request body.

I read a lot of posts on this website, and I know it was often asked, but I can’t execute my request correctly, I still get an error. Here is my code:

uri = URI.parse("http://<my_server_IP_address>:3000/uploads.js")

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.path, initheader = {'X-CSRF-Token' => form_authenticity_token, 'Content-Type' => 'application/octet-stream'})
file = File.new("/home/testFile.txt", 'rb')
request.body = file.read
@response = http.request(request)

As you can see, I set the CSRF token in the header using the method form_authenticity_token, but I still get the 422 error.

Filter chain halted as :verify_authenticity_token rendered or redirected
Completed 422 Unprocessable Entity in 4.7ms (ActiveRecord: 0.0ms)

skip_before_filter :verify_authenticity_token , , .

, ?

. Rails 3.2.16, Ruby 1.9.3-p392 Redmine 2.4.2

+4
1

POST "uploads.js" "uploads.json"?

uri = URI.parse("http://<my_server_IP_address>:3000/uploads.js")

POST uploads.json uploads.xml, -, , .

( , )

+2

Source: https://habr.com/ru/post/1533708/


All Articles