go – How to read file from Body of request-ThrowExceptions

Exception or error:

I am trying to process a file that I receive in POST-request.
So I receive this Body.

Host: 127.0.0.1:8080
Transfer-Encoding: chunked
Connection: close
Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFtdGlncmFAZWR1LmhzZS5ydSIsInJvbGUiOiJ0ZWFjaGVyIiwiaWQiOjAsImV4cCI6MTU1MjAzNjMzNX0.uKH4kZgjl82gQkdoo1XzPrDZeb5wqcN8CWsFupQ0ECg
Connection: close
Content-Type: multipart/form-data
Email: amtigra@edu.hse.ru
Id: 0
Role: teacher

3766
------WebKitFormBoundaryFkRZbuBIZBk3ErsD
Content-Disposition: form-data; name="file"; filename="kdk.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

?�9L�ҙ�sbgٮ|�l!��USh9i�b�r:"y_dl��D���|-N��R"4�2�G�%��Z�4�˝y�7  ë��ɂ���  �� PK    ! �~�q�  �     xl/workbook.xml�T�n�0���w@����%A!UR-R7U[�^V.8�
K�pT-�TΘ.��8tJ�+�C��[0�|�S��tU�J�@$+��*�����-p%��Um����\o[Pd�i4[TBһd7$� �0��'0�rU�T
%�����W�    9
y ���!+��E��fMy�k�����K�5=|�t ��G)�s墙�U��tB��)���,���f�����   �� PK    ! S[�c�  �     xl/worksheets/sheet1.xml��]��0��7��@z�|
JЉʸ;�l�뾖��@ٶ����9��:��8c�sx���=%�{�JkO�d�� o�"�ք�^O��_K{�,�p���t��T����O運��P�,P��m�jǑdC+,��5D
.*��R������t|׍�
�u
��E�#4�dW�Zu"��XA�r�yR��-r�]c^5 �b%S�VYI�5xU¸��Q�ׇ_p´�ߐ*F��PPv����L�����$ㅎ�{��,��$oh���X�A�Ȉ��Ɏ���}�����{>�bOh��VX��������c�L��?=ȋsK��OZR�(0<di{�8����傢l�"&��邖�}���[��1���m����rZ�]�~��W���p�@�S���QI�����+<M?X��P�l��/��}��iJt�rAJ�(�S7u�Py��/c��9@1(�����PZ�-�dׁ��7!�ȅ=�elxs~3
��ڌ�n�ӆ4
��<��� �vx�Yd�g�~4���r��߰X�ZZ%-�>ׄn+pz�x��?��^q�xu���k�BW��*8W������  �� PK    ! M?�,�  �     xl/theme/theme1.xml�Y�o�6��� ��Z�%���l'k��h�=�6m��DC��E�]w0�v��À�i��7-���#%[dL7����$�{��{��C�o<N�s�3NX�qk�<���MI:��G�J�u�@�Q�⎻�ܽ���g�ў�q��O�긱��j�O��kl�Sx6cY��f��4C��7�պ����uR���;��`g$]��k�
���aB��
;=�I_�f������~,\�".�A��ԟ[ݿ^E{�;l5���+�
��d�/6�����$Ζ�Xz�'�1�c�5 �d_Z�G�tn�<[�����Pj$x�\����(�ͻ��q��#��S�-�{H��c2�g3�<$NkHFdlRitH���FRm�����c�6�>>3��Z j!?��Z
4U����-�6���F�n&�fx"��k-2��-(|�֧����Ғ-!�'����evA�͚��p8��ќ8�YY&�Bv�CDUCy;��3�.�9\�膎���@�+���x.'���u/��e�4�,�LCU�i�7�k��I�`�K��6�R��k��B���̺o0!h���j��K�.ZMjW� �"��f��F�]g~��X�r�X�+U᫏�6~�ч��%\��6}��q.��<���eF:�/��Q=�*^+T���UZA�Q�A�j^�W
���Z�h�]�[T��'�d}�vm*S�T�����R������'a}�n�{a���+~�ת���W�Q�?�GA�=|�:g
S2E�I�<*U2�K�����@^c�,  W��hO�8{�MҘ=�W� d��+c��MqEiNTM%�TJb�w��5%ec��Q̱ L�a%��"�~��~�DM�2���a!O��;�4�r�څ3Rx]8ב��щ�~�G�B�FU�p��*V���.�����C
cDwb��=�fX�kfˇ��R�4^�ZiRQ���K�~��}�
����ɻ&$!�YR(��g�t�9'�D�A�p��̪UD0�đ�j���[!�<6Y�Z�/�
�Jh�3<}U{��>��v�d�t c4&��ye ���5�]�_�j��*c�ղ�dd�$�6o��� ���;j?Tw��*O�"�i�"Z�����p��2
��ߍ<R�|��[�s7�Λ��\ok�������6"�s/�2�0nӸ��itC��몇�;X�p�wJpK޳ݛ��]�k�W�����k������?�:B��m˸a��[��k;��?޼�y��   ƿ������?����������z�.`��"-7���):���%k,���v���H����6p8��ig�50��뵚��������y��`��g�4���z�dz��f�/�(8�e�����ts8[5�>{ q��hr���S��h<����O�П���_�Oc?��h3����<�p�﹪��ҷ��a�r&�Z��J�Hp�Bx�>�!g  �� PK    ! �� ��   �     xl/sharedStrings.xmld��J�0���!��f�4���d;��n��LZܷ7����b�|�̜����0��d�{~��83qH��������3*:��R4=?�{u}%�
d�R���k�?d'$o_/�R�u��w�Zo.1�3q�9)XJ:�������Dm���H��*l�LJ�l=Tg_��Ṱ���U���P��9b�#;YPё\����n�bo��,A��7����zM�Y�"�v�BΝ���c�";j
_|xB�+�\r�q��@D'��|۪H���;L����rWt�ȩJ4a�S�1[�^�WƦi�&�b����_�U�R����L
��Y)
�&��o+q�y�F������J0�k��Z�1��\p�ϕ�Ņ��찅<cٳ҄�q��24�5x�C���eY^I���x
�������X��1:k�����5)��P�no�)9ӭ�<$K]*9m�ڀ�%�\F%���_�
             j  xl/styles.xmlPK-     ! ��   ��   �               �  xl/sharedStrings.xmlPK-     !  /-��   �  #             �  xl/worksheets/_rels/sheet1.xml.relsPK-     ! ���B  Q                 docProps/core.xmlPK-     ! ���<�                 �  docProps/app.xmlPK      �  S     
------WebKitFormBoundaryFkRZbuBIZBk3ErsD--

0

So I want to catch only content from this somehow. As reader or as bytes. It doesn’t matter.

I have tried this, but it not worked. I got erroe like “there s no boundary”
So what is the right way to read body if it consist only from file?

func StudentCreateWithExcelPost(w http.ResponseWriter, r *http.Request) {
    mr, err := r.MultipartReader()
    log.Print(err)
    form, err := mr.ReadForm(10 << 20)
    if err != nil {
        log.Print(err)
    }
    fh := form.File["file"]
    excelFile, err := fh[0].Open()
    if err != nil {
        log.Print(err)
    }
}

UPD I’ve just dumped request that I get. So it is really multipart/form-data but without boundary specified. Is it normal?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *