[RUBY] [Rails] How to convert the URI of the image sent by http to https when using Twitter API


My name is yuki. Thanks to DMMWEBCAMP, I am now working as a WEB engineer, gathering my friends to develop services, tutoring programming, and enjoying my engineer life every day.

We also provide support and error questions for those who are aiming from inexperienced, so if you are interested, please contact DM.

Assumptions and problems

I had a problem when I was using Rails as an API and was collecting images using gem twitter. The URI of the image acquired by the twitter API starts with http, and a warning of" unsafe "has appeared on the site where https communication is possible.

By the way, at that time, I was getting this error on the console.

Mixed Content: The page at 'https://hogehoge.com' was loaded over HTTPS, but requested an insecure image 'http://hogehoge.net/hoge.jpg. This content should also be served over HTTPS.

It seems to be called mixed content. At first, I couldn't think of a way to put it together this time, so I managed to do it at the front desk, but I fixed it because it should be done at the back.



  def show
    client = Authorization.init #This is a unique class. Contains client information.
    @data = client.search("#Hashtags you want to collect", result_type: "recent").take(4).collect do |tweet|
        "image": "#{tweet.user.profile_image_url.to_s.sub('http', 'https')}",
        "name": "#{tweet.user.name}",
        "text": "#{tweet.full_text}",
        "tweet_link": "#{tweet.uri}"
    render json: {tweet: @data}

To explain what I'm doing, I collect 4 tweets with a specific hashtag and return them as json data. The important part this time is " image ":" # {tweet.user.profile_image_url.to_s.sub ('http','https')} ",.

This was taught by another engineer, but when I look at ʻuser.profile_image_url` on the rails console, the result is returned in the form of data (?) That I do not understand a little, but by to_s It became a beautiful URL.

It feels like I'm converting by applying the sub method there.

Note that you are using sub instead of gsub. The reason is that if there is a character string "http" in a part (other than the beginning) of the URL of the image, it will be converted incorrectly and the image will not be displayed.

I hope it helps someone.

Recommended Posts

[Rails] How to convert the URI of the image sent by http to https when using Twitter API
How to make the schema of the URL generated by Rails URL helper https
[Rails] How to display the list of posts by category
[Rails 5] How to display the password change screen when using devise
How to implement image posting using rails
How to convert an array of Strings to an array of objects with the Stream API
[Ruby On Rails] How to search the contents of params using include?
[Rails] How to decide the destination by "rails routes"
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
[Rails] How to connect to an external API using HTTP Client (I tried connecting to Qiita API)
How to remove the underline displayed by Rails link_to
How to move to the details screen by clicking the image
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[Rails] How to change the column name of the table
How to request by passing an array to the query with HTTP Client of Ruby
[Rails] How to get the contents of strong parameters
How to judge the click of any area of the image
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
[Rails] How to display an image in the view
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
How to solve the problem when the value is not sent when the form is disabled in rails and sent
How to set environment variables when using Payjp with Rails
[Enum] Let's improve the readability of data by using rails enum
[Rails] Register by attribute of the same model using Devise
When using the constructor of Java's Date class, the date advances by 1900.
How to get the longest information from Twitter as of 12/12/2016
[Rails] How to delete images uploaded by carrierwave (using devise)
Let's summarize how to extend the expiration date of Rails
How to play MIDI files using the Java Sound API
Method definition location Summary of how to check When defined in the project and Rails / Gem
[Rails] How to get the URL of the transition source and redirect
[Swift] How to implement the Twitter login function using Firebase UI ①
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to get the contents of Map using for statement Memorandum
How to reduce JsonModel of Item by using Forge expression BlockState
[Rails] How to omit the display of the character string of the link_to method
[Rails] How to change the page title of the browser for each page
Easy way to create a mapping class when using the API
Replace preview by uploading by clicking the image in file_field of Rails
How to solve the unknown error when using slf4j in Java
[Swift] How to implement the Twitter login function using Firebase UI ②
How to write the view when Vue is introduced in Rails?
[Rails] How to solve the problem that the default image is overwritten when editing without uploading the image [Active Storage]
How to set an image in the drawable Left / Right of a button using an icon font (Iconics)
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery
How to return Rails API mode to Rails
How to decorate the radio button of rails6 form_with (helper) with CSS
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Rails] I was addicted to the nginx settings when using Action Cable.
A story I was addicted to when testing the API using MockMVC
[Rails] About the error when displaying the screen due to the autofocus of the form
Gorigori beginners summarized how to operate the database using rails console Ntiunus
[Java] How to get to the front of a specific string using the String class
How to find the total number of pages when paging in Java
How to constrain the action of the transition destination when not logged in
How to solve the local environment construction of Ruby on Rails (MAC)!
[Swift] How to set an image in the background without using UIImageView.
[Rails] How to solve the time lag of created_at after save method
Change the save destination of the image to S3 in the Rails app. Part 2
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
[Rails] How to convert from erb to haml