urllib2 in python equivalent for ruby

前端 未结 1 604
眼角桃花
眼角桃花 2021-01-23 04:35

I have some code in python that sends an http request in python, but I am trying to figure out how to do it in ruby since my server is rails.

import urllib2, sys         


        
相关标签:
1条回答
  • 2021-01-23 05:37

    Although you're calling the remote server as if it responds to application/x-www-form-urlencoded data, in fact it's just responding to a command in the post body.

    In Python, urllib2.urlopen's data parameter expects a string that's application/x-www-form-urlencoded.

    select * isn't really form encoded so to speak, but it's working for you anyway because the server is interpreting it as a query argument named select * with a None value, or more precisely, the server sees a post body with select * and says "I know how to respond to that command".

    So, a Ruby equivalent to what you're doing is here.

    require 'net/http'
    require 'json'
    
    query = "select *"
    url = "http://new.openbms.org/backend/api/query"
    uri = URI(url)
    response = Net::HTTP.post_form(uri, { query => nil })
    puts JSON.parse(response.body)
    
    0 讨论(0)
提交回复
热议问题