Fork me on GitHub
#cljsrn
<
2023-03-22
>
Dmitry Leonov10:03:43

Hello! Can anyone tell me how to correctly send the file? A photo is taken using react-native-vision-camera, data about it (PhotoFile object) is placed in db Later, the path to the file is obtained, the name, type are filled in, translated into js and sent to the server But I get an error: error photo is invalid

(reg-event-fx
 :recognize-number
 (fn [{:keys [db]} _]
   (let [session-id          (get-in db [:auth :data :session_id])
         service-location-id (get-in db [:auth :data :service_location_id])
         organization-id     (get-in db [:auth :data :organization_id])
         path                (.-path (get-in db [:recognize :photo]))
         name                (last (clojure.string/split path "/"))
         photo-data          (clj->js
                              {:uri  path
                               :name name
                               :type "image/jpeg"})]

     {:http-xhrio {:method          :post
                   :uri             "some-url"
                   :params          {:service_location_id service-location-id
                                     :photo               photo-data}
                   :headers         {:accept          "application/json"
                                     :Content-Type    "multipart/form-data"
                                     :Session-Id      session-id
                                     :Organization-Id organization-id}
                   :format          (ajax/json-request-format)
                   :response-format (ajax/json-response-format {:keyword? true})
                   :on-success      [:recognize-number-success]
                   :on-failure      [:recognize-number-error]}})))

joshmiller16:03:20

It looks like you’re posting local details about the photo file as JSON instead of the photo itself. I.e., the server is getting an object that looks like {"uri": "", "name": "photo.jpg", "type": "image/jpeg"}. I think you’re going to need to get the file data and send it as a multipart attachment.