PATCH Request to Rails API with React Native returns 422

时间:2017-10-24 17:28:53

标签: ruby-on-rails ajax api react-native

I am trying to access an API which which does not require authentication. When I use jQuery, my request works fine, but when requesting in React Native, I get Can't verify CSRF token authenticity. I have tried many ways to post with this token, but can't seem to figure it out.

My code is:

fetch(url, {
  method: 'patch',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: body,
})

2 个答案:

答案 0 :(得分:0)

您需要在请求中设置X-CSRF-Token标头,或禁用跨站点请求伪造(CSRF)保护。以下是一些可以帮助您的链接:

http://api.rubyonrails.org/classes/ActionView/Helpers/CsrfHelper.html#method-i-csrf_meta_tags

Turn off CSRF token in rails 3

答案 1 :(得分:0)

对于CORS请求,请使用" include"允许将凭据发送到其他域的值:

fetch(url, {
  method: 'patch',
  credentials: 'include'
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: body,
})