I'm able to submit both the ticket and the file attachment, but when I try to open the uploaded image with the image viewer I get this error
Error interpreting JPEG image file (Not a JPEG file: starts with 0x2d 0x2d)
and the console output while I'm uploading it shows
Content-Type: "multipart/form-data;
This is how I submit the image, which I'm uploading with a HAML form
connection = Faraday.new(:url => "https://account.zendesk.com/api/v2/uploads.json?filename=image.jpg") do |builder|
builder.basic_auth "email/token", "token"
builder.request :multipart
builder.response :logger
builder.adapter Faraday.default_adapter
end
payload = { 'files[]' => Faraday::UploadIO.new(open(params["images"].first).path, 'image/jpg', "image.jpg", "Content-Disposition" => "file") }
response = connection.post do |req|
req.body = payload
req.headers['Accept'] = 'application/json'
end
rtn_data = JSON.load(response.body)
token = rtn_data['upload']['token']
After I get the token, I submit the ticket the following way:
conn = Faraday.new :url => "https://account.zendesk.com/api/v2/tickets/#{params["ticket_id"]}.json"
conn.basic_auth "email/token", "token"
resp = conn.put do |req|
req.headers['Accept'] = 'application/json'
req.headers['Content-Type'] = 'application/json'
req.body =
'{
"ticket": {
"comment": {
"body": "See screenshot.",
"uploads":["' + token + '"]
}
}
}
end
If I try to change the content type of the file upload to
req.headers['Content-Type'] = 'image/jpg'
I get an error
undefined method 'bytesize' for Hash:0x007fb11e104b78
and if I then change the path to .to_json
Faraday::UploadIO.new(open(params["images"].first).path.to_json, 'image/jpg', "image.jpg", "Content-Disposition" => "file") }
I get another error
No such file or directory @ rb_sysopen - "/tmp/RackMultipart20171124-15501-1n0qio6.jpg"
but if I paste the route in the browser - the picture loads. Any idea what am I doing wrong?