jhawthorn/curl-to-ruby

Doesn't support POST command and the result is completely wrong.

Opened this issue · 1 comments

curl 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept: application/json, text/javascript, */*; q=0.01' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' \
  -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
  -H 'Origin: http://fanyi.youdao.com' \
  -H 'Referer: http://fanyi.youdao.com/' \
  -H 'Accept-Language: en-US,en;q=0.9' \
  -H 'Cookie: SESSION_FROM_COOKIE=www.google.com; OUTFOX_SEARCH_USER_ID=-770416123@119.147.183.50; UM_distinctid=17697740fd4170-04dc0273ef4b3b-163b6152-384000-17697740fd5ad9; JSESSIONID=aaakvUkIMF4LE_Ut7nBAx; OUTFOX_SEARCH_USER_ID_NCOO=1853585491.4869733; ___rl__test__cookies=1608921911154' \
  --data-raw 'i=representative&from=AUTO&to=AUTO&smartresult=dict&client=fanyideskweb&salt=16089219111571&sign=a112f16df706f6ed109f690c3cfe5193&lts=1608921911157&bv=351bfce8c0555a52efc8c9f4762d3359&doctype=json&version=2.1&keyfrom=fanyi.web&action=FY_BY_REALTlME' \
  --compressed \
  --insecure

Results

Body is missing, METHOD should be POST not GET

require 'net/http'
require 'uri'
require 'openssl'

uri = URI.parse("http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule")
request = Net::HTTP::Get.new(uri)
request.content_type = "application/x-www-form-urlencoded; charset=UTF-8"
request["Connection"] = "keep-alive"
request["Pragma"] = "no-cache"
request["Cache-Control"] = "no-cache"
request["Accept"] = "application/json, text/javascript, */*; q=0.01"
request["X-Requested-With"] = "XMLHttpRequest"
request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
request["Origin"] = "http://fanyi.youdao.com"
request["Referer"] = "http://fanyi.youdao.com/"
request["Accept-Language"] = "en-US,en;q=0.9"
request["Cookie"] = "SESSION_FROM_COOKIE=www.google.com; OUTFOX_SEARCH_USER_ID=-770416123@119.147.183.50; UM_distinctid=17697740fd4170-04dc0273ef4b3b-163b6152-384000-17697740fd5ad9; JSESSIONID=aaakvUkIMF4LE_Ut7nBAx; OUTFOX_SEARCH_USER_ID_NCOO=1853585491.4869733; ___rl__test__cookies=1608921911154"

req_options = {
  use_ssl: uri.scheme == "https",
  verify_mode: OpenSSL::SSL::VERIFY_NONE,
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

# response.code
# response.body

You don't specify the method curl should use, so it defaults to GET. Add -X POST to the curl command. Also, the --data-raw flag isn't supported, but since you're not using @ in the message body, you can just use --data.

That would give you a curl command of:

curl -X POST 'http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'Accept: application/json, text/javascript, */*; q=0.01' \
  -H 'X-Requested-With: XMLHttpRequest' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' \
  -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
  -H 'Origin: http://fanyi.youdao.com' \
  -H 'Referer: http://fanyi.youdao.com/' \
  -H 'Accept-Language: en-US,en;q=0.9' \
  -H 'Cookie: SESSION_FROM_COOKIE=www.google.com; OUTFOX_SEARCH_USER_ID=-770416123@119.147.183.50; UM_distinctid=17697740fd4170-04dc0273ef4b3b-163b6152-384000-17697740fd5ad9; JSESSIONID=aaakvUkIMF4LE_Ut7nBAx; OUTFOX_SEARCH_USER_ID_NCOO=1853585491.4869733; ___rl__test__cookies=1608921911154' \
  --data 'i=representative&from=AUTO&to=AUTO&smartresult=dict&client=fanyideskweb&salt=16089219111571&sign=a112f16df706f6ed109f690c3cfe5193&lts=1608921911157&bv=351bfce8c0555a52efc8c9f4762d3359&doctype=json&version=2.1&keyfrom=fanyi.web&action=FY_BY_REALTlME' \
  --compressed \
  --insecure

And the resultant output would be

require 'net/http'
require 'uri'
require 'openssl'

uri = URI.parse("http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/x-www-form-urlencoded; charset=UTF-8"
request["Connection"] = "keep-alive"
request["Pragma"] = "no-cache"
request["Cache-Control"] = "no-cache"
request["Accept"] = "application/json, text/javascript, */*; q=0.01"
request["X-Requested-With"] = "XMLHttpRequest"
request["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
request["Origin"] = "http://fanyi.youdao.com"
request["Referer"] = "http://fanyi.youdao.com/"
request["Accept-Language"] = "en-US,en;q=0.9"
request["Cookie"] = "SESSION_FROM_COOKIE=www.google.com; OUTFOX_SEARCH_USER_ID=-770416123@119.147.183.50; UM_distinctid=17697740fd4170-04dc0273ef4b3b-163b6152-384000-17697740fd5ad9; JSESSIONID=aaakvUkIMF4LE_Ut7nBAx; OUTFOX_SEARCH_USER_ID_NCOO=1853585491.4869733; ___rl__test__cookies=1608921911154"
request.set_form_data(
  "action" => "FY_BY_REALTlME",
  "bv" => "351bfce8c0555a52efc8c9f4762d3359",
  "client" => "fanyideskweb",
  "doctype" => "json",
  "from" => "AUTO",
  "i" => "representative",
  "keyfrom" => "fanyi.web",
  "lts" => "1608921911157",
  "salt" => "16089219111571",
  "sign" => "a112f16df706f6ed109f690c3cfe5193",
  "smartresult" => "dict",
  "to" => "AUTO",
  "version" => "2.1",
)

req_options = {
  use_ssl: uri.scheme == "https",
  verify_mode: OpenSSL::SSL::VERIFY_NONE,
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

# response.code
# response.body

Is this closer to what you wanted?