简体   繁体   English

从rake任务发送GET请求

[英]send GET request from rake task

I want to get json from some url in rake task. 我想从rake任务中的某些网址获取json。 how I can achive that? 我怎样才能做到这一点?

I used this code 我用了这段代码

require 'open-uri'
content = open(my_url).read
logger.info content

but it throw WebMock::NetConnectNotAllowedError it saying stub this request but how I can get responce from stub_request dont know 但它抛出WebMock::NetConnectNotAllowedError它说存根此请求,但我如何才能从stub_request获得响应不知道

from controller it works fine 从控制器工作正常

You need to place the webmock gem in the test group: 您需要将webmock gem放入测试组中:

# Gemfile
# ...
group :test do
  gem 'webmock'
end

Or allow webmock to connect to the net if you are invoking the rake task in a test: 或者,如果您在测试中调用rake任务,则允许webmock连接到网络:

WebMock.allow_net_connect!

https://github.com/bblimke/webmock https://github.com/bblimke/webmock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM