请帮助我理解,我做错了什么? 我需要从我的rails应用程序向api发出POST请求,我首先尝试使用gem“faraday”然后使用“net / http”。来自服务器的响应相同。帐户创建,但出错:
Net :: HTTPBadResponse(标题行格式错误)
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
通过Postman成功完成此请求,因此api的端点正在运行。
请帮我理解标题有什么问题? 提前谢谢!
更新:
这可能是问题吗?
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">]
答案 0 :(得分:0)
如果你想要一个容易混合到一个类/模块的http lib,而不是我推荐httparty而不是Faraday
或只是普通的Net::HTTP
。
require 'httparty' # Requires go at the top of the file. Not in class body.
class MyApiClient
include HTTParty
base_uri 'test.example.com'
# Tells Httparty to send JSON for all requests.
format :json
# @see https://robots.thoughtbot.com/ruby-2-keyword-arguments
def initialize(client_id: ENV["client_id"], client_secret: ENV["client_secret"])
@options = {
body: {
client_id: client_id,
client_secret: client_secret
}
}
end
# @return [HTTParty::Response]
def create_account(first_name:, last_name:, email:, password:)
# ActiveSupport method that creates a copy so that we don't
# alter the instance options as a side effect.
opts = @options.deep_dup
opts[:body].merge!(
email: email,
password: password,
first_name: first_name,
last_name: last_name
)
self.class.post('/endclient/api/account', opts)
end
end
好的一点是,httparty
将完成所有工作,将参数解析为JSON和发送正确的标头。它还负责构建URI的沉闷工作。
请注意,方法签名略有不同:
client = MyApiClient.new
response = client.create_account(
first_name: 'John',
last_name: 'Doe',
email: 'john.doe@example.com',
password: 'abcd1234'
)
答案 1 :(得分:0)
API端存在问题,标题中的符号错误。