首页 \ 问答 \ 如何在ruby中的http请求中包含标头(how do I include a header in an http request in ruby)

如何在ruby中的http请求中包含标头(how do I include a header in an http request in ruby)

让下面的代码工作:

uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/")
response = Net::HTTP.get_response(uri)

现在我还需要在其中传递带有此标记哈希的标头:

token: "fjhKJFSDHKJHjfgsdfdsljh"

我找不到有关如何执行此操作的任何文档。 我该怎么做?


Have the below code working:

uri = URI.parse("http://www.ncdc.noaa.gov/cdo-web/api/v2/datasets/")
response = Net::HTTP.get_response(uri)

Now I also need to pass a header with this token hash in it:

token: "fjhKJFSDHKJHjfgsdfdsljh"

I cannot find any documentation on how to do this. How do I do it?


原文:https://stackoverflow.com/questions/38211085
更新时间:2024-02-16 06:02

最满意答案

您可以使用Data.Functor.Compose将嵌套的Data.Functor.Compose / Applicatives视为单个Functor / Applicative:

ghci> import Data.Functor.Compose
ghci> getCompose (Compose [pure (+2), pure (*2)] <*> Compose [Just 10, Just 20])
[Just 12,Just 22,Just 20,Just 40]

但在这种情况下,( ComposegetCompose )新型噪声可能不值得。


You can use Data.Functor.Compose to treat nested Functors/Applicatives as a single Functor/Applicative:

ghci> import Data.Functor.Compose
ghci> getCompose (Compose [pure (+2), pure (*2)] <*> Compose [Just 10, Just 20])
[Just 12,Just 22,Just 20,Just 40]

But perhaps the (Compose, getCompose) newtype noise is not worth it in this case.

相关问答

更多
  • 我想提出一个答案,提请注意上面八月的评论: 实际上并不是这样。 发生了什么事情是地图的类型被概括为覆盖Haskell 1.3中的Functor。 即,在Haskell 1.3中,fmap被称为地图。 然后,这个更改在Haskell 1.4中被还原,并引入了fmap。 这种变化的原因是教学性的; 当教给Haskell初学者时,非常一般的地图类型的错误信息更难理解。 在我看来,这不是解决问题的正确方法。 Haskell 98被一些Haskellers(包括我)退后一步,以前的版本定义了一个更抽象和一致的库。 好 ...
  • 关键在这里: fmap (*) 3 :: (Functor f, Num a, Num (f a)) => f (a -> a) ^^^^^^^^^ 在Haskell中,数字文字被重载,因此只要b具有Num实例, 3就可以有任何类型b 。 编译器进行以下分析: 3具有约束Num b某种类型Num b fmap类型为Functor f => (a -> c) -> fa -> fc 所以*有类型a -> c ,3有类型fa *具有类型Num ...
  • 您忘记在Branch节点中包含a s上应用函数f ,否则您只将函数应用于叶子上。 而且,你忘记了Empty构造函数的情况。 instance Functor Tree where fmap f Empty = Empty fmap f (Leaf x) = Leaf (f x) fmap f (Branch a left right) = Branch (f a) (fmap f left) (fmap f right) You are forgetting to apply the func ...
  • 这是因为xs是Rose a的列表。 也许这更清楚: fmap f (x :> xs) = (f x) :> map (fmap f) xs ^^^ 当然,对于列表, map = fmap 。 那是: 将f应用于x 。 对于xs ,将函数fmap f映射到列表xs It's because xs is a list of Rose a. Perhaps this is clearer: fmap f (x :> xs) = (f x) :> map ( ...
  • 我不认为这是“坏”(除非它在该特定monad的fmap中没有优化),但我不喜欢它的冗长。 我宁愿为此定义自己的运算符, <$$>已被建议或<&>与$ / & duality匹配(另请参阅任何人翻转(<$>) ): infixl 1 <&> (<&>) = flip (<$>) findDlls dir = listDirectoryAbs dir >>= filterM predicate3 <&> filter predicate2 >>= filterM predicate1 <&> f 顺 ...
  • Haskell类型类基于一阶逻辑分辨率。 类型变量上的类型类约束是谓词(如果您曾尝试在该逻辑系统中尝试使用需要类型名称的类型类名称,则可能会看到错误消息,指示此情况)。 Haskell在整个程序中需要为每个(谓词,类型)对提供唯一的解决方案,因此您将无法在Int上创建两个不同的Functor实例。 围绕这个的标准方法,例如在Monoid类中,可以提供求和或产品的Monoid类,具体取决于你如何定义你想要使用的monoidal运算符,就是在你想要的类的具体类型上提供newtype包装器有不同的实例。 因此,对 ...
  • 我们通常不能在fmap方面实现return 。 Monad比Functor更强大。 但是,作为练习,我们可以尝试提出这个问题:如果有的话,第二个操作是否可以实现fmap return ? 我们可以通过查看类型来攻击这个问题。 (我们将使用Applicative类中的pure而不是return它们基本上是相同的操作。) fmap :: Functor f => (a -> b) -> f a -> f b pure :: Applicative f => a -> f a 嗯,这可能是一种可能的方式,如果我 ...
  • 如果您翻转类型,可能会更容易看到: (e -> a) -> (a -> b) -> (e -> b) 我们可以把e变成a ,把a变成b 。 那么我们怎样才能把e变成b呢? 不要过分关注“提升”; 使用Functor实例,发现实现的最佳方法就是遵循类型。 It may be easier to see if you flip the types around: (e -> a) -> (a -> b) -> (e -> b) We can turn an e into an a, and an a int ...
  • 您可以使用Data.Functor.Compose将嵌套的Data.Functor.Compose / Applicatives视为单个Functor / Applicative: ghci> import Data.Functor.Compose ghci> getCompose (Compose [pure (+2), pure (*2)] <*> Compose [Just 10, Just 20]) [Just 12,Just 22,Just 20,Just 40] 但在这种情况下,( Compo ...
  • 正如你在回答的评论中提到的那样, join和>>=有类似的故事。 当有几种语义等价的方法来定义某些东西时,最好总是选择最有效和务实的方式。 Haskell旨在编写代码,而不是为了证明事物(尽管如此,不幸的是Haskell仍然不会成为非常流行的编程语言)。 如果starAp有默认实现,几乎没有人会实现它(正如现在发生在Monad类型类中的>> )。 但<*>是非常有用的操作。 它用于应用程序和megaparsec解析器很多( megaparsec , attoparsec , optparse-applica ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)