title: Rails API ActiveStorage DirectUpload produce 422 Error InvalidAuthenticityToken
body: <p>I have a problem with Rails API app active storage. I have React from where i want to upload file.</p>
  
  <pre><code>import React from "react";
  import {DirectUpload} from "activestorage";
  
  class SignIn extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        file: null
      };
      this.handleFileChange = this.handleFileChange.bind(this);
      this.handleFileSubmit = this.handleFileSubmit.bind(this);
    }
  
    handleFileChange(e){
      this.setState({file: e.target.files[0]})
    }
  
    handleFileSubmit(){
      const upload = new DirectUpload(this.state.file, "/rails/active_storage/direct_uploads");
  
      upload.create((error, blob) =&gt; {
        if(error){
          console.log(error)
        } else {
          console.log(blob)
        }
      })
    }
  
    render() {
  
      return (
        &lt;React.Fragment&gt;
            &lt;Form&gt;
            &lt;Form.Item&gt;
              &lt;Input type="file" onChange={this.handleFileChange}/&gt;
            &lt;/Form.Item&gt;
  
            &lt;Form.Item&gt;
              &lt;Button type="primary" htmlType="submit"&gt;
                Register
              &lt;/Button&gt;
            &lt;/Form.Item&gt;
          &lt;/Form&gt;
        &lt;/React.Fragment&gt;
      );
    }
  }
  
  </code></pre>
  
  <p>But on submit i got error <code>Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)</code></p>
  
  <pre><code>Started POST "/rails/active_storage/direct_uploads" for 127.0.0.1 at 2019-05-09 22:59:54 +0200
  Processing by ActiveStorage::DirectUploadsController#create as JSON
    Parameters: {"blob"=&gt;{"filename"=&gt;"file.jpg", "content_type"=&gt;"image/jpeg", "byte_size"=&gt;27095, "checksum"=&gt;"8u95dXg39vap1Cq/2fgfbg=="}, "direct_upload"=&gt;{"blob"=&gt;{"filename"=&gt;"file.jpg", "content_type"=&gt;"image/jpeg", "byte_size"=&gt;27095, "checksum"=&gt;"8u95dXg39vap1Cq/2fgfbg=="}}}
  Can't verify CSRF token authenticity.
  Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
  
  
  
  ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
  
  actionpack (5.2.3) lib/action_controller/metal/request_forgery_protection.rb:211:in `handle_unverified_request'
  actionpack (5.2.3) lib/action_controller/metal/request_forgery_protection.rb:243:in `handle_unverified_request'
  devise (4.6.2) lib/devise/controllers/helpers.rb:255:in `handle_unverified_request'
  actionpack (5.2.3) lib/action_controller/metal/request_forgery_protection.rb:238:in `verify_authenticity_token'
  activesupport (5.2.3) lib/active_support/callbacks.rb:426:in `block in make_lambda'
  activesupport (5.2.3) lib/active_support/callbacks.rb:198:in `block (2 levels) in halting'
  actionpack (5.2.3) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in &lt;module:Callbacks&gt;'
  activesupport (5.2.3) lib/active_support/callbacks.rb:199:in `block in halting'
  activesupport (5.2.3) lib/active_support/callbacks.rb:513:in `block in invoke_before'
  </code></pre>
  
  <p>I set in application_controller.rb <code>protect_from_forgery with: :null_session</code> but i still got error.</p>