Please help me understand, what i'm doing wrong? I need to make POST request to api from my rails app, i tried to do it firstly with gem "faraday" and then with "net/http". Response from server the same. Account creates, but gives error:
Net::HTTPBadResponse (wrong header line format)
class Connect
require 'uri'
require 'net/http'
require 'net/https'
require 'faraday'
def initialize(email, password, account)
@base_url = "https://example.com"
@client_id = ENV["client_id"]
@client_secret = ENV["client_secret"]
@email = email
@password = password
@account = account
end
# method with net/http
def create_account
@toSend = {
first_name: @account[:first_name],
last_name: @account[:last_name],
client_id: @client_id,
client_secret: @client_secret,
email: @email,
password: @password
}.to_json
uri = URI.parse("#{@base_url}/endclient/api/account")
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(
uri.path,
initheader = {
'Content-Type' =>'application/json',
'Accept' =>'application/json'
}
)
req.body = "#{@toSend}"
res = https.request(req)
end
# faraday method
def make_account
conn = Faraday.new(:url => "#{@base_url}") do |faraday|
faraday.response :logger
faraday.adapter Faraday.default_adapter
end
params = { first_name: @account[:first_name],
last_name: @account[:last_name],
client_id: @client_id,
client_secret: @client_secret,
email: @email,
password: @password
}.to_json
response = conn.post do |req|
req.url '/endclient/api/account'
req.headers['Content-Type'] = 'application/json'
req.headers['Accept'] = 'application/json'
req.body = params
end
end
end
Done this request with success via Postman, so endpoint of the api is working.
Please help me understand what's wrong with headers ? Thanks in advance!
UPDATE:
May be problem in this ?
Cannot render console with content type application/jsonAllowed content types: [#<Mime::Type:0x00000003661658 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x00000003660fc8 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x0000000365ca68 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]