How to define the header ['content-type'] = 'application / json' in ruby

require 'net/http'

require 'rubygems'

require 'json'

url = URI.parse('http://www.xyxx/abc/pqr')

resp = Net::HTTP.get_response(url) # get_response takes an URI object

data = resp.body

puts data

this is my code in ruby, is giving me data in xml form.

rest api return xml data by default , and json if header content-type is application/json.

but i want data in json form.for this i have to set header['content-type']='application/json'.

but i do not know , how to set header with get_response get json data.

def post_test
  require 'net/http'
  require 'json'
  @host = ''
  @port = '8080'
  @path = "/restxxx/abc/xyz"

  request =, initheader = {'Content-Type' =>'application/json'})
  response =, @port).start {|http| http.request(request) }

  puts "Response #{response.code} #{response.message}: #{response.body}"