특정 목록을 검색하기 위해 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)))
반환 여기
+
은
%2B
으로 표시됩니다. 따라서
iphone+5
은
iphone%2B5
으로 표시됩니다.
iphone%2B5
을 URI의
iphone+5
으로 바꾸면 올바른 데이터를 반환합니다.
질문은 HTTParty로 GET 요청을 할 때 기호가 %2B
으로 변환되는 것을 어떻게 방지합니까?
참고 : 이것은 here에 대해 the Tin Man이 말한 내용 일 것입니다.