使用ruby net / http获取HTTPBadRequest 400

时间:2016-01-26 08:44:47

标签: ruby api httprequest net-http chatbot

我正在尝试用Ruby编写一个Chatbot。我目前正面临着http请求的问题。这甚至是做出请求的正确方法,还是我必须在代码的其他部分中搜索我的错误?我也在使用代理,这会影响http请求吗?

这是我的要求:

def makeRequest(urlToCheck)
    uri = URI.parse('https://somesite/api/lookup/url')
    http = Net::HTTP::new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Post.new('https://somesite/api/lookup/url')
    request.initialize_http_header({"X-PMAN-KEY" => "fdsgdfsgdf"}).to_json
    request.content_type = "application/x-www-form-urlencoded"
    data = URI.encode_www_form({"url" => "#{urlToCheck}","userAgent" => "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ 40.0.2214.115 Safari/537.36"})
    request.body = data
    @response= http.request(request)
    puts request

#<Net::HTTPBadRequest 400 Bad Request readbody=true>

0 个答案:

没有答案