我正在尝试上传图片。这是我从网站文档中收到的Curl代码
curl -X POST
'https://my.craftar.net/api/v0/image/?api_key=123456789abcdefghijk123456789abcdefghijk'
-F "item=/api/v0/item/4fe672886ec142f6ab6d72d54acf046f/"
-F "file=@test.png"
我尝试用&net; http / http / post / multipart'来解决这个问题。
File.open("./test.png") do |png|
data = {"item" => "/api/v0/item/2fb8cc16002742ccbbecddc186b1e131"}
uri = URI.parse('https://my.craftar.net/api/v0/image/?api_key=09d29c3801932aab983892565e731ee3e5c0879e')
headers = { 'Content-Type' =>'application/json'}
req = Net::HTTP::Post::Multipart.new uri.request_uri,
{"file" => UploadIO.new(png, "image/png", "image.png")}.merge(data),
headers
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
res = http.request(req)
puts "result:"
puts res.body
end
我将此代码基于' net / http / post / multipart'的文档。 但它没有工作我遇到了很多错误。而且我无法弄清楚我做错了什么。
现在我从CraftAR
收到错误{" message":"预期的多部分/表格数据;边界= LT; ..>内容但得到了application / json; boundary = ----------- RubyMultipartPost。"," code":" WRONG_CONTENT_TYPE"}}
答案 0 :(得分:0)
标题和参数的顺序错误。咨询the source here
固定电话应为
File.open("./test.png") do |png|
req = Net::HTTP::Post::Multipart.new(uri.request_uri,
{"file" => UploadIO.new(png, "image/png", "image.png")}.merge(data))
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end