Ruby - Post 请求的编码主体

Ruby - Encoding Body Of A Post Request

我正在尝试将由 hash 等不同参数组成的正文转换为如下编码格式:

params = {
  "senderid" => "abc",
  "username" => "xyz",
  "password" => "1234",
  "Response" => "Y",
  "mtype" => "TXT",
  "dltentityid" => "15058",
  "tmid" => "11101",
  "subdatatype" => "M",
  "dlttempid" => "345690",
  "dlttagid" => "",
  "msgdata" => {
    "data" => [
      {
      "mobile" => "9000099999",
      "message" => "Thank for showing your preference for the loan requirement for your dream home at Happinest Kalyan. We have a host financial partners who are offering home loans at competitive rates. Login access the details of financial partners on abc."
      }
    ]
  }
}

  url                     = URI('http://www.smsjust.com/sms/user/urlsms_json.php')
  http                    = Net::HTTP.new(url.host, url.port)
  request                 = Net::HTTP::Post.new(url)
  request["Content-Type"] = 'application/x-www-form-urlencoded'
  request.body            = URI.encode_www_form(params)
  response                = http.request(request)

接收方收到如下request.body。这里 params 的 URI 编码对 params 进行如下编码,这不起作用并给出输出 wrong json format:

"senderid=abc&username=xyz&password=%qwe3&Response=Y&mtype=TXT&dltentityid=12658&tmid=150000010001&subdatatype=M&dlttempid=150092&dlttagid=&msgdata=%7B%22data%22%3D%3E%5B%7B%22mobile%22%3D%3E%229998973369%22%2C+%22message%22%3D%3E%22Thank+for+showing+your+preference+for+the+loan+requirement+for+your+dream+home+at+Happinest+Kalyan.+We+have+a+host+financial+partners+who+are+offering+home+loans+at+competitive+rates.+Login+access+the+details+of+financial+partners+on+%24project_name.%22%7D%5D%7D"

并且 postman 中的相同 post 请求将参数的 URI 编码翻译如下 有效 - 以下是预期的格式,其中它应该发送给接收者:

"username=abc&password=%qweeCS3&senderid=qas&mtype=TXT&msgdata=%7B%22data%22%3A%5B%7B%22mobile%22%3A%229812593882%22%2C%22message%22%3A%22Thank%20for%20showing%20your%20preference%20for%20the%20loan%20requirement%20for%20your%20dream%20home%20at%20Happinest%20Kalyan.%20We%20have%20a%20host%20financial%20partners%20who%20are%20offering%20home%20loans%20at%20competitive%20rates.%20Login%20access%20the%20details%20of%20financial%20partners%20on%20%24project_name.%22%7D%2C%7B%22mobile%22%3A%228849593882%22%2C%22message%22%3A%22Sixth%20message%22%7D%5D%7D&subdatatype=M&Response=Y&dlttempid=15092&dltentityid=1502658&tmid=15010001&dlttagid="  

已经尝试过: 我试过使用 CGI.escape 但也不起作用。

对于上述问题的任何帮助将不胜感激。

我找到了如下所示的解决方案:

params = {
  "senderid" => "abc",
  "username" => "xyz",
  "password" => "1234",
  "Response" => "Y",
  "mtype" => "TXT",
  "dltentityid" => "15058",
  "tmid" => "11101",
  "subdatatype" => "M",
  "dlttempid" => "345690",
  "dlttagid" => ""  
}

msgdata = {
  "data" => [
    {
    "mobile" => "9000099999",
    "message" => "Thank for showing your preference for the loan requirement for your dream home at Happinest Kalyan. We have a host financial partners who are offering home loans at competitive rates. Login access the details of financial partners on abc."
    }
  ]
}.to_json

  url = URI('http://www.smsjust.com/sms/user/urlsms_json.php')
  http = Net::HTTP.new(url.host, url.port);
  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = "application/x-www-form-urlencoded"
  request_body = (params.each { |k, v| params[k] = CGI.escape(v.to_s) }).to_query
  request.body = "#{request_body}&msgdata=#{CGI.escape(msgdata.to_s)}"

这里的问题是将所有参数转换为 CGI.escape 而不是 URI.encode_www_form 除了 msgdata 字段。

由于 msgdata 是一个字符串,它应该在正文中作为 json 字符串发送 - 因此将其单独转换为 json --> 应用 CGI.escape然后将其附加到 request_body.

希望这对解决这个问题的人有所帮助 !!!