How to remove the bom from http response
What is bom?
The byte order mark (BOM) is a Unicode character used to signal the endianness (byte order) of a text file or stream. Its code point is U+FEFF. BOM use is optional, and, if used, should appear at the start of the text stream.
How to know the response is bom encoded or not:
To know whether the response is bom encoded or not you just need to see the response if the response look something like this:
now how to convert this bom encoded response to json response in ruby
Thanks for reading this blog!!!
The byte order mark (BOM) is a Unicode character used to signal the endianness (byte order) of a text file or stream. Its code point is U+FEFF. BOM use is optional, and, if used, should appear at the start of the text stream.
How to know the response is bom encoded or not:
To know whether the response is bom encoded or not you just need to see the response if the response look something like this:
BASE_URL = 'http://www.claritin.com/webservice/allergyforecast.php?zip='
resp = Net::HTTP.get_response(URI("#{BASE_URL}10007")).body
"\xEF\xBB\xBF\"[{\\\"pollenForecast\\\":{\\\"zip\\\":\\\"10007\\\",\\\"city\\\":\\\"NEW YORK\\\",\\\"state\\\":\\\" NY\\\",\\\"forecast\\\":[1.1,0.1,0.8,0.6],\\\"pp\\\":\\\"Mixed Trace.\\\",\\\"timestamp\\\":\\\"11\\/14\\/2015 12:00:00 AM\\\"},\\\"weatherForecast\\\":{\\\"date\\\":\\\"2015-11-13T07:00:00-0500\\\",\\\"city\\\":\\\"NEW YORK\\\",\\\"state\\\":\\\" NY\\\",\\\"zip\\\":\\\"10007\\\",\\\"forecast\\\":[{\\\"lowF\\\":\\\"42\\\",\\\"highF\\\":\\\"47\\\",\\\"iconNight\\\":\\\"33\\\",\\\"skyDay\\\":0,\\\"skyNight\\\":33,\\\"phraseNight\\\":\\\"Mostly Clear\\\",\\\"date\\\":\\\"2015-11-13T07:00:00-0500\\\",\\\"iconDay\\\":null,\\\"phraseDay\\\":null},{\\\"lowF\\\":\\\"40\\\",\\\"highF\\\":\\\"49\\\",\\\"iconNight\\\":\\\"33\\\",\\\"skyDay\\\":24,\\\"skyNight\\\":33,\\\"phraseNight\\\":\\\"Mostly Clear\\\",\\\"date\\\":\\\"2015-11-14T07:00:00-0500\\\",\\\"iconDay\\\":\\\"24\\\",\\\"phraseDay\\\":\\\"Mostly Sunny\\/Wind\\\"},{\\\"lowF\\\":\\\"47\\\",\\\"highF\\\":\\\"59\\\",\\\"iconNight\\\":\\\"31\\\",\\\"skyDay\\\":32,\\\"skyNight\\\":31,\\\"phraseNight\\\":\\\"Clear\\\",\\\"date\\\":\\\"2015-11-15T07:00:00-0500\\\",\\\"iconDay\\\":\\\"32\\\",\\\"phraseDay\\\":\\\"Sunny\\\"},{\\\"lowF\\\":\\\"47\\\",\\\"highF\\\":\\\"64\\\",\\\"iconNight\\\":\\\"29\\\",\\\"skyDay\\\":32,\\\"skyNight\\\":29,\\\"phraseNight\\\":\\\"Partly Cloudy\\\",\\\"date\\\":\\\"2015-11-16T07:00:00-0500\\\",\\\"iconDay\\\":\\\"32\\\",\\\"phraseDay\\\":\\\"Sunny\\\"},{\\\"lowF\\\":\\\"52\\\",\\\"highF\\\":\\\"60\\\",\\\"iconNight\\\":\\\"26\\\",\\\"skyDay\\\":30,\\\"skyNight\\\":26,\\\"phraseNight\\\":\\\"Cloudy\\\",\\\"date\\\":\\\"2015-11-17T07:00:00-0500\\\",\\\"iconDay\\\":\\\"30\\\",\\\"phraseDay\\\":\\\"Partly Cloudy\\\"},{\\\"lowF\\\":\\\"56\\\",\\\"highF\\\":\\\"60\\\",\\\"iconNight\\\":\\\"11\\\",\\\"skyDay\\\":30,\\\"skyNight\\\":11,\\\"phraseNight\\\":\\\"Showers\\\",\\\"date\\\":\\\"2015-11-18T07:00:00-0500\\\",\\\"iconDay\\\":\\\"30\\\",\\\"phraseDay\\\":\\\"Partly Cloudy\\\"}]},\\\"result\\\":true}]\""then this response is bom encoded.
now how to convert this bom encoded response to json response in ruby
JSON.parse(eval (resp))this line will return the Json response like this:
[{"pollenForecast"=>{"zip"=>"10007", "city"=>"NEW YORK", "state"=>" NY", "forecast"=>[1.1, 0.1, 0.8, 0.6], "pp"=>"Mixed Trace.", "timestamp"=>"11/14/2015 12:00:00 AM"}, "weatherForecast"=>{"date"=>"2015-11-13T07:00:00-0500", "city"=>"NEW YORK", "state"=>" NY", "zip"=>"10007", "forecast"=>[{"lowF"=>"42", "highF"=>"47", "iconNight"=>"33", "skyDay"=>0, "skyNight"=>33, "phraseNight"=>"Mostly Clear", "date"=>"2015-11-13T07:00:00-0500", "iconDay"=>nil, "phraseDay"=>nil}, {"lowF"=>"40", "highF"=>"49", "iconNight"=>"33", "skyDay"=>24, "skyNight"=>33, "phraseNight"=>"Mostly Clear", "date"=>"2015-11-14T07:00:00-0500", "iconDay"=>"24", "phraseDay"=>"Mostly Sunny/Wind"}, {"lowF"=>"47", "highF"=>"59", "iconNight"=>"31", "skyDay"=>32, "skyNight"=>31, "phraseNight"=>"Clear", "date"=>"2015-11-15T07:00:00-0500", "iconDay"=>"32", "phraseDay"=>"Sunny"}, {"lowF"=>"47", "highF"=>"64", "iconNight"=>"29", "skyDay"=>32, "skyNight"=>29, "phraseNight"=>"Partly Cloudy", "date"=>"2015-11-16T07:00:00-0500", "iconDay"=>"32", "phraseDay"=>"Sunny"}, {"lowF"=>"52", "highF"=>"60", "iconNight"=>"26", "skyDay"=>30, "skyNight"=>26, "phraseNight"=>"Cloudy", "date"=>"2015-11-17T07:00:00-0500", "iconDay"=>"30", "phraseDay"=>"Partly Cloudy"}, {"lowF"=>"56", "highF"=>"60", "iconNight"=>"11", "skyDay"=>30, "skyNight"=>11, "phraseNight"=>"Showers", "date"=>"2015-11-18T07:00:00-0500", "iconDay"=>"30", "phraseDay"=>"Partly Cloudy"}]}, "result"=>true}]
Thanks for reading this blog!!!
Comments
Post a Comment