How to Fix Bad Uri Is Not Uri

How to fix bad URI is not URI

This url is not valid, but it works in browser because browser itself is less strict about special characters like :, /, etc.

You should encode your URI first

encoded_url = URI.encode(url)

And then parse it

URI.parse(encoded_url)

How to fix bad URI(is not URI?) in ruby on rails :URI::InvalidURIError

When you use HTTParty.get you pass the city and state that are "dynamic" values, as in this case city is "new york", the url is being created with a whitespace, and that's why you get the error:

URI::InvalidURIError (bad URI(is not URI?):
http://api.wunderground.com/api/apikey/forecast10day/q/new
york/NY.json)

You could use URI.encode(url) and this way, the whitespace would be "converted" to %20, like:

http://api.wunderground.com/api/apikey/forecast10day/q/new%20york/NY.json

Which is a "valid" url to work with. So you can tweak a little bit your fetch_week_forecast method, like:

def fetch_week_forecast(city, state)
HTTParty.get(URI.encode("http://api.wunderground.com/api/apikey/forecast10day/q/#{city}/#{state}.json"))
end

how to fix this little bad URI(is not URI?) error?

Your problem is that you are sending sybmol and not value. Verify the function calling Find method and assure it is value and not symbol.

Fixing RSPEC URI::InvalidURIError: bad URI(is not URI?): error for post request test

You are making the request in your RSpec the wrong way:

context 'when the url is valid' do
# Change this line below
before { post :create, url: "www.google.com" }

it 'returns a status code of 201' do
expect(response).to have_http_status(201)
end
end

bad URI(is not URI?):

You do not pass objects in a URI. You can only have attributes. Specifically, the { and } have no meaning in the context of a URI attribute.

You need a parameter for each field in the donation that you want to pass.

For example:

...?token=foo&donation-amount=101&donation-comment=Ordered

URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers

Controller tests inherit from ActionController::TestCase, while your test
inherits from ActionDispatch::IntegrationTest. So you're using an integration test and not a controller test.

The error is:

http://www.example.com:80index

That doesn't look right, does it? ;-)

The solution is to use a full path:

get '/index'

Remember, integration tests aren't really tied to any specific controller (or anything else, for that matter). They test the integration of several components in your application. So if you're testing the index action of a UserController you'd probably need to use /users/index.

If you intended to make a controller test and not an integration test, you want to set the correct superclass. Using get :index (for the index method) should work fine then.

URI::InvalidURIError (bad URI(is not URI?): ):

I got into trouble with URI.split (returning this error), I don't know if this helps you, but I will post here some warnings for also someone else having this error:

  1. Check your url is not nil, and it's a valid one.
  2. Do URI.encode(url) before URI.parse (to avoid special characters)
  3. Do strip to the string you pass to URI.parse (to avoid leading and trailing whitespaces).

All in one:

uri = URI.parse(URI.encode(url.strip))

Related resource: http://www.practicalguile.com/2007/09/15/raising-uriinvalidurierror-from-a-perfectly-valid-uri/



Related Topics



Leave a reply



Submit