Can't verify token authenticity token error while trying to attach a new file to a document
Added by Kevin Tomba over 10 years ago
Version used:
Redmine 2.4.2
Ruby 1.9.3
Rails 3.2.16
I'm working on a plugin and I would like to attach a new file to an existing document by clicking a link instead of using the original form, to do this I'm trying to recreate HTTP Post requests generated when the original form is used.
I followed the instructions here: http://www.redmine.org/projects/redmine/wiki/Rest_api#Attaching-files
So here is the beggining of my code:
uri = URI.parse("http://<server_address_IP>:3000/uploads.js")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => 'application/octet-stream'})
ajkdajk jdkjdkjk = File.new("/home/testFile.txt", 'rb')
request.body = @file.read
@response = http.request(request)
As explained in the previous link, I set the content-type to application/octet-stream and I put the file content in the request body
When I execute the code, I get the following error:
Filter chain halted as :verify_authenticity_token rendered or redirected Completed 422 Unprocessable Entity in 2.6 ms (ActiveRecord: 0.0ms)
I noticed by sniffing the network that when uploads work with the original form an CSRF authenticity token is always present in the request body as well.
How can I get this token (so I can add it in my body request)?
Is it possible to attach file the way I want to do it? If yes, could give me a little bit help, I'm stuck on this for a few days now.
Thanks
Replies (1)
RE: Can't verify token authenticity token error while trying to attach a new file to a document - Added by Kevin Tomba over 10 years ago
I've updated my code like this:
uri = URI.parse("http://<server_address_IP>: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)
And I'm still getting the error, do you have any idea?