400 bad call in ruby ​​but not curl

I am trying to make a request to write https in ruby ​​and it works in curl, but I get 400 requests in ruby ​​and it is hard for me to understand why.

Here is the ruby ​​code:

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

This gives me an answer #<Net::HTTPBadRequest 400 BAD_REQUEST readbody=true>.

But the curl equivalent works just fine:

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

Any ideas what I'm doing wrong?

+5
source share
3 answers

You must provide the full URL:

req = Net::HTTP::Post.new("https://api.parse.com/1/push")
+4
source

For me, the problem is not to make an SSL request. As soon as I installed the following line, everything worked:

http.use_ssl = true

This is not an OPs problem, but I still post an error message, and this thread is one of google's best hits for the error.

+5
source
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  

.

0

All Articles