REST post informations not received with multipart/form-data












1















Using Restify & Mongoose, I built a basic REST API. Everything is working fine as long as I send JSON content with application/json as a content.



But now I need to send an image along with content, so I switched to multipart/form-data for a specific route. And it don't seem to get the content at all.



Here’s the basic setup sent to my API with Insomnia:



Content with insomnia



Pretty simple. The Mongodb Model only require filename and url at the moment (I’ll deal with the file itself once this will work).



And this is what I get as a response from the server:



{
"code": "Internal",
"message": "Media validation failed: url: Path `url` is required., filename: Path `filename` is required."
}


So it’s saying it didn't got any of the fields content.



The Timeline shows that the fields have been sent:



> POST /medias HTTP/1.1
> Host: localhost:3030
> User-Agent: insomnia/6.0.2
> Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
> Accept: */*
> Content-Length: 221
| --X-INSOMNIA-BOUNDARY
| Content-Disposition: form-data; name="filename"
| test.jpg
| --X-INSOMNIA-BOUNDARY
| Content-Disposition: form-data; name="url"
| https://images.test.com/test.jpg
| --X-INSOMNIA-BOUNDARY--
* We are completely uploaded and fine
< HTTP/1.1 500 Internal Server Error
< Server: TestServer
< Content-Type: application/json
< Content-Length: 125
< Connection: Keep-Alive
< Content-MD5: KI89enLnYv9oMJxg5k4MXA==
< Date: Sun, 12 Aug 2018 22:50:31 GMT
< Request-Id: b20babc1-9f1b-466b-af7c-13d385cd8023
< Response-Time: 6
* Received 125 B chunk
* Connection #65 to host localhost left intact


Here’s my function:



server.post('/medias', (req, res, next) => {
if (!req.is('multipart/form-data')) {
return next(
new errors.InvalidContentError(`Expects 'multipart/form-data'`)
)
}

let data = req.body || {}

let media = new Media(data)
media.save((err) => {
if (err) {
return next(new errors.InternalError(err.message))
next()
}

res.send(201)
next()
})
})


A console.log on the req.body reveal that it’s empty. I’ve consoled the req itself to see if the datas could be stored anywhere else, but I couldn’t find anything.



What did I missed here? Thank you in advance.










share|improve this question



























    1















    Using Restify & Mongoose, I built a basic REST API. Everything is working fine as long as I send JSON content with application/json as a content.



    But now I need to send an image along with content, so I switched to multipart/form-data for a specific route. And it don't seem to get the content at all.



    Here’s the basic setup sent to my API with Insomnia:



    Content with insomnia



    Pretty simple. The Mongodb Model only require filename and url at the moment (I’ll deal with the file itself once this will work).



    And this is what I get as a response from the server:



    {
    "code": "Internal",
    "message": "Media validation failed: url: Path `url` is required., filename: Path `filename` is required."
    }


    So it’s saying it didn't got any of the fields content.



    The Timeline shows that the fields have been sent:



    > POST /medias HTTP/1.1
    > Host: localhost:3030
    > User-Agent: insomnia/6.0.2
    > Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
    > Accept: */*
    > Content-Length: 221
    | --X-INSOMNIA-BOUNDARY
    | Content-Disposition: form-data; name="filename"
    | test.jpg
    | --X-INSOMNIA-BOUNDARY
    | Content-Disposition: form-data; name="url"
    | https://images.test.com/test.jpg
    | --X-INSOMNIA-BOUNDARY--
    * We are completely uploaded and fine
    < HTTP/1.1 500 Internal Server Error
    < Server: TestServer
    < Content-Type: application/json
    < Content-Length: 125
    < Connection: Keep-Alive
    < Content-MD5: KI89enLnYv9oMJxg5k4MXA==
    < Date: Sun, 12 Aug 2018 22:50:31 GMT
    < Request-Id: b20babc1-9f1b-466b-af7c-13d385cd8023
    < Response-Time: 6
    * Received 125 B chunk
    * Connection #65 to host localhost left intact


    Here’s my function:



    server.post('/medias', (req, res, next) => {
    if (!req.is('multipart/form-data')) {
    return next(
    new errors.InvalidContentError(`Expects 'multipart/form-data'`)
    )
    }

    let data = req.body || {}

    let media = new Media(data)
    media.save((err) => {
    if (err) {
    return next(new errors.InternalError(err.message))
    next()
    }

    res.send(201)
    next()
    })
    })


    A console.log on the req.body reveal that it’s empty. I’ve consoled the req itself to see if the datas could be stored anywhere else, but I couldn’t find anything.



    What did I missed here? Thank you in advance.










    share|improve this question

























      1












      1








      1








      Using Restify & Mongoose, I built a basic REST API. Everything is working fine as long as I send JSON content with application/json as a content.



      But now I need to send an image along with content, so I switched to multipart/form-data for a specific route. And it don't seem to get the content at all.



      Here’s the basic setup sent to my API with Insomnia:



      Content with insomnia



      Pretty simple. The Mongodb Model only require filename and url at the moment (I’ll deal with the file itself once this will work).



      And this is what I get as a response from the server:



      {
      "code": "Internal",
      "message": "Media validation failed: url: Path `url` is required., filename: Path `filename` is required."
      }


      So it’s saying it didn't got any of the fields content.



      The Timeline shows that the fields have been sent:



      > POST /medias HTTP/1.1
      > Host: localhost:3030
      > User-Agent: insomnia/6.0.2
      > Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
      > Accept: */*
      > Content-Length: 221
      | --X-INSOMNIA-BOUNDARY
      | Content-Disposition: form-data; name="filename"
      | test.jpg
      | --X-INSOMNIA-BOUNDARY
      | Content-Disposition: form-data; name="url"
      | https://images.test.com/test.jpg
      | --X-INSOMNIA-BOUNDARY--
      * We are completely uploaded and fine
      < HTTP/1.1 500 Internal Server Error
      < Server: TestServer
      < Content-Type: application/json
      < Content-Length: 125
      < Connection: Keep-Alive
      < Content-MD5: KI89enLnYv9oMJxg5k4MXA==
      < Date: Sun, 12 Aug 2018 22:50:31 GMT
      < Request-Id: b20babc1-9f1b-466b-af7c-13d385cd8023
      < Response-Time: 6
      * Received 125 B chunk
      * Connection #65 to host localhost left intact


      Here’s my function:



      server.post('/medias', (req, res, next) => {
      if (!req.is('multipart/form-data')) {
      return next(
      new errors.InvalidContentError(`Expects 'multipart/form-data'`)
      )
      }

      let data = req.body || {}

      let media = new Media(data)
      media.save((err) => {
      if (err) {
      return next(new errors.InternalError(err.message))
      next()
      }

      res.send(201)
      next()
      })
      })


      A console.log on the req.body reveal that it’s empty. I’ve consoled the req itself to see if the datas could be stored anywhere else, but I couldn’t find anything.



      What did I missed here? Thank you in advance.










      share|improve this question














      Using Restify & Mongoose, I built a basic REST API. Everything is working fine as long as I send JSON content with application/json as a content.



      But now I need to send an image along with content, so I switched to multipart/form-data for a specific route. And it don't seem to get the content at all.



      Here’s the basic setup sent to my API with Insomnia:



      Content with insomnia



      Pretty simple. The Mongodb Model only require filename and url at the moment (I’ll deal with the file itself once this will work).



      And this is what I get as a response from the server:



      {
      "code": "Internal",
      "message": "Media validation failed: url: Path `url` is required., filename: Path `filename` is required."
      }


      So it’s saying it didn't got any of the fields content.



      The Timeline shows that the fields have been sent:



      > POST /medias HTTP/1.1
      > Host: localhost:3030
      > User-Agent: insomnia/6.0.2
      > Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
      > Accept: */*
      > Content-Length: 221
      | --X-INSOMNIA-BOUNDARY
      | Content-Disposition: form-data; name="filename"
      | test.jpg
      | --X-INSOMNIA-BOUNDARY
      | Content-Disposition: form-data; name="url"
      | https://images.test.com/test.jpg
      | --X-INSOMNIA-BOUNDARY--
      * We are completely uploaded and fine
      < HTTP/1.1 500 Internal Server Error
      < Server: TestServer
      < Content-Type: application/json
      < Content-Length: 125
      < Connection: Keep-Alive
      < Content-MD5: KI89enLnYv9oMJxg5k4MXA==
      < Date: Sun, 12 Aug 2018 22:50:31 GMT
      < Request-Id: b20babc1-9f1b-466b-af7c-13d385cd8023
      < Response-Time: 6
      * Received 125 B chunk
      * Connection #65 to host localhost left intact


      Here’s my function:



      server.post('/medias', (req, res, next) => {
      if (!req.is('multipart/form-data')) {
      return next(
      new errors.InvalidContentError(`Expects 'multipart/form-data'`)
      )
      }

      let data = req.body || {}

      let media = new Media(data)
      media.save((err) => {
      if (err) {
      return next(new errors.InternalError(err.message))
      next()
      }

      res.send(201)
      next()
      })
      })


      A console.log on the req.body reveal that it’s empty. I’ve consoled the req itself to see if the datas could be stored anywhere else, but I couldn’t find anything.



      What did I missed here? Thank you in advance.







      node.js rest restify insomnia






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 12 '18 at 23:21









      EmmanuelBeziatEmmanuelBeziat

      227114




      227114
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Well, I finally just got it with base64 encoding.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51813646%2frest-post-informations-not-received-with-multipart-form-data%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Well, I finally just got it with base64 encoding.






            share|improve this answer




























              1














              Well, I finally just got it with base64 encoding.






              share|improve this answer


























                1












                1








                1







                Well, I finally just got it with base64 encoding.






                share|improve this answer













                Well, I finally just got it with base64 encoding.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 23:25









                EmmanuelBeziatEmmanuelBeziat

                227114




                227114






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51813646%2frest-post-informations-not-received-with-multipart-form-data%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin