2014-07-23 5 views
0

특정 목록을 검색하기 위해 GET 요청을하고 있습니다. 테스트중인 키워드는 iphone 5입니다.'+'이 (가) '% 2B'(으)로 변환되는 이유는 무엇입니까?

일 :

m = Marktplaats.new 
m.search(keyword: 'iphone 5') 

나는 관련이없는 데이터를 얻을. 나는 그것이 요청과 함께 할 수있는 뭔가가 생각

https://api.marktplaats.nl/api3/ads.json?oauth_token=1me6jq76h8t6rim747m7bketkd&api_ver=3.7&session=ebc565b8-659f-40f6-9d0a-96986f1d1595&screenWidth=62&screenHeight=111&app_ver=Android3.1.0&q=iphone%2B5&searchOnTitleAndDescription=false&showListings=true&categoryId=1953&page=1&size=30&sortBy=DEFAULT&sortOrder=DESCENDING&showHistograms=true 

:

require 'httparty' 

class Marktplaats 
    include HTTParty 

    base_uri('https://api.marktplaats.nl') 

    def search(keyword) 

    # Debug 
    puts search_query_params(keyword) 
    puts last_uri(self.class.get('/api3/ads.json', query: search_query_params(keyword))) 

    self.class.get('/api3/ads.json', query: search_query_params(keyword)) 
    end 

    def view(urn) 
    self.class.get("/api3/ads/#{urn}.json", query: default_query_params) 
    end 

    def categories 
    self.class.get('/api3/categories.json', query: default_query_params) 
    end 

    protected 

    def default_query_params 
     { 
     oauth_token: '1me6jq76h8t6rim747m7bketkd', 
     api_ver: '3.7', 
     session: 'ebc565b8-659f-40f6-9d0a-96986f1d1595', 
     screenWidth: '62', 
     screenHeight: '111', 
     app_ver: 'Android3.1.0' 
     } 
    end 

    def search_query_params(args = {}) 
     search_params = { 
     q: split_keyword(args[:keyword]), 
     searchOnTitleAndDescription: args[:search_on_title_and_description] || 'false', 
     showListings: args[:show_listings] || 'true', 
     categoryId: args[:category_id] || '1953', 
     page: args[:page] || '1', 
     size: args[:size] || '30', 
     sortBy: args[:sort_by] || 'DEFAULT', 
     sortOrder: args[:sort_order] || 'DESCENDING', 
     showHistograms: args[:show_histograms] || 'true' 
     } 
     merge_params(default_query_params, search_params) 
    end 

    def merge_params(params1, params2) 
     params1.merge(params2) 
    end 

    def split_keyword(keyword) 
     keyword.gsub(' ', '+') 
    end 

    def last_uri(last_request) 
     last_request.request.last_uri 
    end 
end 

puts search_query_params(keyword) 반환 :

{:oauth_token=>"1me6jq76h8t6rim747m7bketkd", :api_ver=>"3.7", :session=>"ebc565b8-659f-40f6-9d0a-96986f1d1595", :screenWidth=>"62", :screenHeight=>"111", :app_ver=>"Android3.1.0", :q=>"iphone+5", :searchOnTitleAndDescription=>"true", :showListings=>"false", :categoryId=>"1953", :page=>"1", :size=>"30", :sortBy=>"DEFAULT", :sortOrder=>"DESCENDING", :showHistograms=>"true"} 

last_uri(self.class.get('/api3/ads.json', query: search_query_params(keyword))) 반환 여기

내가 사용하고 코드입니다 URI. 키워드 간 기호 +%2B으로 표시됩니다. 따라서 iphone+5iphone%2B5으로 표시됩니다. iphone%2B5을 URI의 iphone+5으로 바꾸면 올바른 데이터를 반환합니다.

질문은 HTTParty로 GET 요청을 할 때 기호가 %2B으로 변환되는 것을 어떻게 방지합니까?

참고 : 이것은 here에 대해 the Tin Man이 말한 내용 일 것입니다.

답변

0

클래스에서 #split_keyword(keyword) 메서드를 제거하여 더 이상 사용하지 않으므로이 문제를 해결했습니다.

단어 사이에 + 기호로 수동으로 바꾸지 않아도 된 것 같습니다. 내가한다면, 그들은 작동하지 않는 것 같아 %2B로 변환됩니다.

수동 교체없이 ' '%20으로 변환되어 작동합니다.