我正在使用发送JSON的外部API。
当我将相同的JSON复制到Paw(类似邮递员的工具)时,它会起作用。
使用滑轨时出现400 bad request
错误。
我认为这是一个编码问题,因为当我想解析400个响应正文时,我需要做
response.body.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
然后请求和响应是:
请求
{"searchable":{"default":true},"c_body":{"ja-JP":{"_type":"markup_text","source":"<div class=\"cms-content-wrapper\"><div class=\"cms-page-module\" data-template-id=\"552100319\" data-template-name=\"PEP_Features_Carousel\" data-page-module-id=\"408407719\" rank=\"cptiuz\"><section class=\"sk-component sk-component--pep-feature-slider\" data-slider-id=\"\" id=\"\">\r\n <div class=\"sk-carousel sk-carousel__emotional sk-carousel-load\" data-sk-carousel-limit=\"{'large':1, 'medium':1, 'small':1}\" data-loop=\"true\">\r\n\t<div class=\"sk-carousel-inner\">\r\n\t\t<ul class=\"sk-carousel-wrapper\" style=\"left: -100%; transform:none;\">\r\n \r\n \r\n \r\n <li>\r\n <div class=\"sk-carousel__item\" data-gallery-item=\"\">\r\n <div class=\"sk-copy--text-wrapper\">\r\n <div class=\"sk-copy--text\">\r\n <div class=\"sk-pre-header\"><p>ストッケ エクスプローリーの主な特徴</p>\n</div>\r\n <h3>より近くで絆を感じる</h3>\r\n \r\n <div class=\"sk-copy--large\">公園からコーヒーショップまで、エクスプローリーはお子さまと親御さまの絆を感じられる、最適な高さにお子さまの位置を調節します。</div>\r\n \r\n <div .........
响应正文
{"_v":"19.5","fault":{"arguments":{"body":"{\"searchable\":{\"default\":true},\"c_body\":{\"ja-JP\":{\"_type\":\"markup_text\",\"source\":\"<div class=\\\"cms-content-wrapper\\\"><div class=\\\"cms-page-module\\\" data-template-id=\\\"552100319\\\" data-template-name=\\\"PEP_Features_Carousel\\\" data-page-module-id=\\\"408407719\\\" rank=\\\"cptiuz\\\"><section class=\\\"sk-component sk-component--pep-feature-slider\\\" data-slider-id=\\\"\\\" id=\\\"\\\">\\r\\n <div class=\\\"sk-carousel sk-carousel__emotional sk-carousel-load\\\" data-sk-carousel-limit=\\\"{'large':1, 'medium':1, 'small':1}\\\" data-loop=\\\"true\\\">\\r\\n\\t<div class=\\\"sk-carousel-inner\\\">\\r\\n\\t\\t<ul class=\\\"sk-carousel-wrapper\\\" style=\\\"left: -100%; transform:none;\\\">\\r\\n \\r\\n \\r\\n \\r\\n <li>\\r\\n <div class=\\\"sk-carousel__item\\\" data-gallery-item=\\\"\\\">\\r\\n <div class=\\\"sk-copy--text-wrapper\\\">\\r\\n <div class=\\\"sk-copy--text\\\">\\r\\n <div class=\\\"sk-pre-header\\\"><p>???????????? ???????????????????????????????????????</p>\\n</div>\\r\\n <h3>?????????????????????
在响应中一直滚动到最右边时,有很多???日语文字在哪里。这就是为什么我假设其为编码问题。但我似乎无法修复。
这是我的代码:
def patch_content(content_asset_document, content_asset_id, instance_url)
puts :patch_content
token = get_token
header = {
"Authorization" => "Bearer #{token.access_token}",
"Content-Type" => "application/json; charset=UTF-8"
}
url = "#{instance_url}/libraries/lib/content/#{content_asset_id}"
uri = URI.parse(url)
request = Net::HTTP::Patch.new(uri.request_uri, header)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.ssl_version = :TLSv1_2
request.body = content_asset_document.to_json.to_s
response = http.request(request)
File.open("request.txt", "w") { |file| file.puts request.body }
yield response.code.to_i, response
end