向具有 CSRF 保护的 Ruby on Rails 应用程序发出 POST 请求时出现 404 错误

米马尔蒂

所以有这个开源应用程序将引用字符串解析为 BibTex 格式,我正在尝试使用 Python Requests 将其功能集成到我的项目中。

我知道该表单具有 CSRF 保护,因此我确保将令牌作为 POST 请求的一部分包含在内,但这并没有成功,返回 404 错误:

from requests import Session
from lxml import html

citations = ["107 F. Scarpa, S. Blain, T. Lew, D. Perrott, M. Ruzzene and J. Yates, Elastic buckling of hexagonal chiral cell honeycombs, Composites, Part A, 2007, 38(2), 280–289"]
s = Session()
url = 'https://anystyle.io/'
page = s.get(url)
tree = html.fromstring(page.content)
csrf_token = tree.xpath("/html/head/meta[4]/@content")[0]
response = s.post(
    headers = {
        'Origin': url,
        'Referer': url,
        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0',
        'Accept': 'application/json, text/plain, */*',
        'Accept-Language': 'en-US,en;q=0.5',
        'Content-Type': 'application/json;charset=utf-8',
        'Connection': 'keep-alive',
        'X-CSRF-TOKEN': csrf_token
    },
    url = url,
    params = (
        ('format', 'json'),
    ),
    json = {
        'input': citations
    },
    cookies = {
        '_any_style_session': s.cookies.get('_any_style_session')
    }
)
response.json() 

我想知道的是,上面的代码是否正确解决了 CSRF 问题以返回成功的请求,或者应用程序的设计方式是否可以防止自动请求。

安德烈·凯斯利

添加/parse到 POST 网址:

from requests import Session
from lxml import html

citations = ["107 F. Scarpa, S. Blain, T. Lew, D. Perrott, M. Ruzzene and J. Yates, Elastic buckling of hexagonal chiral cell honeycombs, Composites, Part A, 2007, 38(2), 280–289"]
s = Session()
url = 'https://anystyle.io'
page = s.get(url)
tree = html.fromstring(page.content)
csrf_token = tree.xpath("/html/head/meta[4]/@content")[0]
response = s.post(
    headers = {
        'X-CSRF-TOKEN': csrf_token  # <-- only this header is needed
    },
    url = url + '/parse',           # <-- add '/parse' here!
    params = (
        ('format', 'json'),
    ),
    json = {
        'input': citations
    },
    # cookies= are not needed:
)
print( response.json() )

印刷:

[[['citation-number', '107'], ['author', 'F.'], ['author', 'Scarpa,'], ['author', 'S.'], ['author', 'Blain,'], ['author', 'T.'], ['author', 'Lew,'], ['author', 'D.'], ['author', 'Perrott,'], ['author', 'M.'], ['author', 'Ruzzene'], ['author', 'and'], ['author', 'J.'], ['author', 'Yates,'], ['title', 'Elastic'], ['title', 'buckling'], ['title', 'of'], ['title', 'hexagonal'], ['title', 'chiral'], ['title', 'cell'], ['title', 'honeycombs,'], ['journal', 'Composites,'], ['journal', 'Part'], ['journal', 'A,'], ['date', '2007,'], ['volume', '38(2),'], ['pages', '280–289']]]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Angular 2向Spring支持的Java应用程序发送POST请求时出现CSRF问题

Ruby on Rails CSRF保护表单

当我向 api 服务器发出 post 请求时收到 404 错误

如何在Rails应用程序的ruby中添加404错误页面?

为什么在尝试将Ruby on Rails应用程序部署到Heroku时出现错误?

向Rails应用程序发送JSON POST请求

使用 turbolinks 和 ruby on rails 时发生 CSRF 令牌错误

为实现 dynamoDb 功能而编写的 Spring Boot 应用程序的 POST 请求期间 Postman 出现错误 404

使用具有 action 属性的多个 post 方法时出现错误 403 csrf 令牌

在Express.js上使用Axios向Spotify API发出POST请求时出现错误400

假客户端向微服务发出POST / PUT请求时出现错误403

尝试向 Nest JS 服务器发送特定 POST 请求时,为什么会出现 404 错误?

具有跨域的angular js应用程序中的CSRF保护

通过Angular应用程序的Node.js POST请求返回错误404“未找到”

Rails:发出POST请求时无法验证CSRF令牌的真实性

单页应用程序的Rails CSRF保护(反应,角度,余烬)

从后端应用程序保护 NodeJS 中的 POST 请求

如何从在其他端口上运行的本地AngularJS应用程序向本地Rails应用程序发出请求?

导航到Rails应用程序中的页面会向不同的页面发出多个请求

通过 Nodejs 上的 npm 请求模块发出 POST 请求时出现 CERT_HAS_EXPIRED 错误,但证书有效

从asp.net核心应用程序发出DELETE请求时出现405错误

通过CSRF令牌通过多部分形式发出的POST请求在生产中给出404错误,但在本地主机上工作

向在 ngrok 上运行的应用程序发送发布请求时出现 502 错误

从 Android 应用程序向 Laravel 发送请求时出现 419 错误代码

在Heroku上部署Ruby on Rails应用程序时出现“ postgresql gem未加载”错误

无法将 Ruby on Rails 应用程序部署到 AWS,出现与“mini_racer”相关的错误

为什么在Ruby on Rails应用程序中出现未定义的方法错误?

评论 ruby on rails 电子商务网络应用程序时出现错误 400

Ruby on rails facebook canvas应用程序没有路线匹配[POST]“ /”