2017-12-08 14 views
1

웹 사이트에서 데이터를 가져 오기 위해 HTTP 요청을하려고합니다. 이렇게하려면 사용자 지정 헤더를 사용해야합니다. 사용자 정의 헤더를 잘 추가하여 우편 배달부에 요청하면 모든 것이 올바르게 작동합니다. 맞춤 헤더를 삭제하면 정상적으로 표시되는 {"error": "content not found"}이라는 오류 메시지가 표시됩니다.HTTP 요청은 우편 배달부에서 작동하지만 레일에서는 작동하지 않습니다.

레일즈에서 같은 요청을하려고했습니다. 이것은 내 코드입니다.

uri = URI.parse("https://www.itqi.com/api/master-descriptor") 
http = Net::HTTP.new(uri.host, uri.port) 
http.use_ssl = true 
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
req = Net::HTTP::Get.new(uri.path) 
req["itqi-key"] = '************************' 
response = http.request(req) 

이 경우 사용자 지정 헤더를 추가했습니다. 그러나 이것에도 불구하고 데이터가 없습니다.이 오류 메시지는 {"error": "content not found"}이며 내 사용자 정의 헤더가 고려되지 않은 것과 같습니다. 나는 성공하지 못한 채 많은 가능성을 시험해 보았습니다.

저를 도울 수있는 누군가 있습니까? 나는 아이디어가 부족하다.

미리 조언 해 주셔서 감사합니다.

안부,

편집 여기

은 패러데이 내 HTTPS 요청입니다 :

conn = Faraday.new(:url => 'https://www.itqi.com', :ssl => {:verify => false}) 
response = conn.get do |req| 
    req.url '/api/master-descriptor' 
    req.headers['Content-Type'] = 'application/json' 
    req.headers['itqi-key'] = '********************' 
end 

그리고 여기에 서버 응답입니다 :

응답 : #https : //www.itqi.com/api/master-descriptor> @ request = # @request_headers = { "U ser-Agent "=>"Faraday v0.12.2 ","Content-Type "=>"application/json ","itqi-key "=>"**************** ****** "} @ ssl = # @ response = # @response_headers = {"date "=>"Wed, 13 12 월 13 일 08:25:53 GMT ","server "=>"Apache/2.4.10 (데비안) ","content-length "=>"29 ","connection "="close ","content-type "=>"application/json; 캐릭터 = UTF-8 "} @ 상태 = 404 @ reason_phrase ="항상

같은 문제 :(

>> 찾을 수 없습니다 "************** ********** 솔루션 *************************

일부 연구를 마친 후, .. 헤더 키가 레일에서 대소 문자를 구분 그래서이 문제를 해결하기 위해, 나는 그없이 소문자를 넣어 레일을 강제로했다, 레일 체계적를 대문자로

내 솔루션이있다 :.

conn = Faraday.new(url: 'https://www.itqi.com', ssl: {verify: false}) do |conn| 
    conn.request :json 
    conn.response :json, :content_type => /\bjson$/ 
    conn.adapter Faraday.default_adapter 
end 
response = conn.get do |req| 
    req.url '/api/master-descriptor' 
    itqi_key = CaseSensitiveString.new('itqi-key') 
    req.headers[itqi_key] = '************************' 
end 

class CaseSensitiveString < String 
    def downcase 
    self 
    end 
    def capitalize 
    self 
    end 

    def to_s 
    self 
    end 
end 

답변

0

코드가 http://ruby-doc.org/stdlib-2.0.0/libdoc/net/http/rdoc/Net/HTTP.html#label-Setting+Headers의 예와 같이 보이지 않습니다.

하지만 패러데이 (https://github.com/lostisland/faraday)로 전환하면 더 행복해집니다.

+0

답변 해 주셔서 감사합니다. 나는 패러데이를 사용했다. 그러나 나는 똑같은 문제가있다. { "error": "content not found"}라는 오류 메시지가 다시 나타납니다. 이 헤더를 어떻게 추가 할 수 있습니까? { "itqi-key"=> '**********************'} – Wivi0

+0

코드는 어떻게 생겼습니까? 그것은 다음과 같은 것을해야합니다. (case-sentence) : 'conn.get do | req | req.headers [ 'Content-Type'] = 'application/json' end' –

+0

패러데이 및 서버 응답 코드를 포함하도록 내 게시물을 편집했습니다.내 머리글을 고려하지 않은 이유를 알아 냈습니까? 당신의 도움을 주셔서 감사합니다. – Wivi0