繁体   English   中英

400红宝石的坏请求但不卷曲

[英]400 bad request in ruby but not curl

我正在尝试在ruby中发布https发布请求并且它在curl中工作,但是我在ruby中得到了400个错误的请求,而我却无法弄清楚原因。

下面是红宝石代码:

require 'rubygems'
require 'net/https'
require 'uri'
require 'json'

uri = URI.parse("https://api.parse.com/1/push")
http =  Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

req = Net::HTTP::Post.new(uri.host)
req['Content-Type'] = "application/json"
req['X-Parse-Application-Id'] = "abc123"
req['X-Parse-REST-API-Key'] = "abc123"

req.body = {:data => {:alert => "sup"}, :channels => ["notifications"]}.to_json

resp = http.start{|http| http.request(req)}

puts resp.inspect

这给了我一个#<Net::HTTPBadRequest 400 BAD_REQUEST readbody=true>响应。

但卷曲等效工作正常:

curl -X POST \
  -H "X-Parse-Application-Id: abc123" \
  -H "X-Parse-REST-API-Key: abc123" \
  -H "Content-Type: application/json" \
  -d '{
    "channels": [
      "notifications"
    ],
    "data": {
      "alert": "sup?"
    }
  }' \
  https://api.parse.com/1/push

我有什么想法我做错了吗?

对我来说问题是没有通过SSL发出请求。 一旦我设置了以下行,一切正常:

http.use_ssl = true

这不是OP的问题,但我仍然发帖,因为错误信息是相同的,这个线程是错误的顶级谷歌点击之一。

你应该指定完整的网址:

req = Net::HTTP::Post.new("https://api.parse.com/1/push")
def get_job(job_name)

        puts "Posting job #{job_name} to get test update order service"

        get_job_xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://os.uk/Test/GetUpdateOrders/1.0\">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:GetTestUpdateOrdersReq>
             <ContractGroup>abc</ContractGroup>
             <ProductID>myproduct</ProductID>
             <Reference>#{job_name}</Reference>
          </ns:GetTestUpdateOrdersReq>
       </soapenv:Body>
    </soapenv:Envelope>"

        @http = Net::HTTP.new(@server, @port)


        request = Net::HTTP::Post.new(('/XISOAPAdapter/MessageServlet?senderParty=&senderService=faceNamespace=http://myco.uk/Test/GetUpdateOrders/1.0'), initheader = {'Content-Type' => 'text/xml'})
        request.basic_auth(@username, @password)
        request.content_type='text/xml'
        request.body = get_job_xml
        response = @http.request(request)


      end  

我已经定义了这样的方法和它的工作方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM