3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Faradayを使ってローカルの開発環境から外部のAPIに接続できない

Posted at

外部APIを叩くときにFaradayを使って実装していたときに、本番環境ではうまくいくのにローカルの環境ではエラーでアクセスできなかった。
##コード

conn = Faraday.new
res  = conn.post do |req|
        req.url                               'https://sample.com/api/sample'
        req.headers['X_APP_API_TOKEN']      = 'amenboakainaaiueo'
        req.headers['Accept']               = 'application/json'
        req.headers['Content-Type']         = 'application/json'
        req.body                            = '{friday: day13th}'
      end

##エラー内容

:ConnectionFailed: Failed to open TCP connection to :80 (Connection refused - connect(2) for nil port 80)

意訳:httpじゃアクセスさせない、出直しなさい

##解決方法

conn = Faraday.new('https://sample.com/api/sample')
res  = conn.post do |req|
        req.url                               'https://sample.com/api/sample'
        req.headers['X_APP_API_TOKEN']      = 'amenboakainaaiueo'
        req.headers['Accept']               = 'application/json'
        req.headers['Content-Type']         = 'application/json'
        req.body                            = '{friday: day13th}'
      end

Faraday.newするときにhttpsでアクセスするよーってことを明示したら行けました。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?