2.3 HTTP Post Request with fetch() - Working with Data and APIs in JavaScript

Поделиться
HTML-код
  • Опубликовано: 26 авг 2024
  • 💻github.com/Cod...
    We are now in Module #2! In the previous module, we focused on client-side JavaScript. We now will learn the basics of server-side programming with Node.
    2.3 Let's take data from the client and send it to the server. This video covers routing, JSON parsing, and HTTP POST requests with fetch().
    🎥 NEXT LESSON: • 2.4 Saving to a Databa...
    🎥 PREVIOUS LESSON: • 2.2 Geolocation Web AP...
    🎥 FULL COURSE: • Working with Data and ...
    🚂 Website: thecodingtrain....
    💖 Patreon: / codingtrain
    🛒 Store: www.designbyhu...
    📚 Books: www.amazon.com...
    🎥 Coding Challenges: • Coding Challenges
    🎥 Intro to Programming: • Start learning here!
    🔗 p5.js: p5js.org
    🔗 Processing: processing.org
    📄 Code of Conduct: github.com/Cod...

Комментарии • 323

  • @matthewburson2908
    @matthewburson2908 5 лет назад +152

    My day is always better when I see new Coding Train videos.

  • @garretgeorge9803
    @garretgeorge9803 4 года назад +60

    You can not imagine how long i searched for such a tutorial. Thank you. Just Thank you.

  • @user-qq7ch
    @user-qq7ch 4 года назад +60

    This guy seriously makes the best tutorial content on RUclips. I've learned SO much, all thanks to this guy. His videos are legit worth $1000s in semesters at my university. And no pressure of being graded!

  • @MrBelleblaas
    @MrBelleblaas 4 года назад +16

    I've seen several instructional videos from you and I'm still impressed by the clarity of this despite the fact that you're very quick, but clear about what it's all about.
    Sometimes it looks like an Attention Deficit Hyperactivity Disorder (ADHD), but this is a natural gift in your case.
    Thank you very much and go on with it.

  • @aliaksandrhn1
    @aliaksandrhn1 4 года назад +1

    This was the most comprehensive nuts-and-bolts explanation of fetch API I could find. Most other videos don't even come close. Thank you.

  • @RootEntry
    @RootEntry 4 года назад +10

    oh just great. I tried learning this and googled questions and tried it out for an hour. And then when I finished I saw this video.
    THanks youtube.

  • @pidynamicslimited8395
    @pidynamicslimited8395 4 года назад +9

    I've struggled with this for weeks. And finally, I found The Coding Train videos and they made it all so simple. Thank you man!

  • @user-qq7ch
    @user-qq7ch 4 года назад +8

    Learning back-end coding at the moment. These videos are seriously awesome. Well-taught, good production, and helps so so much.

  • @richardmelkonian5681
    @richardmelkonian5681 3 года назад

    Can I just say. I first started coding about 4 years ago as a hobby, these videos were always a constant source of joy and fun for me back then. I'm now a full-stack dev at a blockchain based startup, 4 years later I still love your videos. THANK YOU!! :)

  • @ShizaanSil
    @ShizaanSil 3 года назад

    I have been struggling with this server side stuff for DAYS, i go to coding train and 20 minutes later i know all i needed and been trying to learn for so long. You sir, are incredible

  • @aravindvenkateswaran5294
    @aravindvenkateswaran5294 3 года назад

    I have no words, stumbled upon this and learnt quite possibly the most important way of client server interactions I have been trying to learn for months.

  • @akkitty22
    @akkitty22 2 года назад

    Great clarity for a person who really struggles to comprehend concepts delivered by stackoverflow people, you put it visually, legibly and repeatedly so the audience can review, re-review at their own pace. Thank you for helping everyone.

  • @jollywgiant1760
    @jollywgiant1760 3 года назад +1

    I have watched this video so much for when I need help on fetch's! You are an invaluable human, thank you for your brilliant work!

  • @saikatdas3645
    @saikatdas3645 Год назад

    Realllly man... Tonsss of thanks❤️❤️❤️❤️❤️🙇🙇.. after browsing through the internet, after going through several post cannot figure out this simple feature, that u explain in this short video.. i am really glad 😊

  • @lindatochukwu3081
    @lindatochukwu3081 4 года назад +4

    I love the way u teach and the happiness u display, makes me remember when I was still a kid, thanks for that enthusiasm, smiles

  • @rinovethamoses124
    @rinovethamoses124 5 лет назад +9

    Your videos are so useful for me.
    I have learned a lot about javascript through your videos.
    Thank you

  • @hananfadah
    @hananfadah 7 месяцев назад

    This guy is the best teacher heaven has sent for developers 😊

  • @ABDULMANAN-el8gr
    @ABDULMANAN-el8gr 4 года назад

    Unfortunate this Video has only ~80k views. The Best Explanation ever.. I kind of love coding now because of you . Thank You so much

  • @JeanLuemusic
    @JeanLuemusic 2 года назад

    The way you explain things makes me fall in love with coding. thank you so much.

  • @CodyPellom
    @CodyPellom 4 года назад +4

    Coding Train guy, I really appreciate your hard work. I know these videos take a lot of work to produce and you are the best for putting these out here. Also, you should say your name more, so we can buy your book!!!

  • @drakongames5417
    @drakongames5417 2 года назад

    so easy to understand. after so many hours of finding my mistake and failing you saved the day. thx sir

  • @sunbrogilgamesh
    @sunbrogilgamesh 2 года назад

    Man, I'm getting crazy just to understand what headers are, but this video made many things clearer to me than I expected!

  • @emirichiba4507
    @emirichiba4507 3 года назад

    This video is simply amazing!!! I finally understood how the server and the client communicate each other, which I couldn't understand for more than a month in the course where I paid 800 euro!

  • @alex_topunov
    @alex_topunov 3 года назад +3

    Thank you, Daniel! Your energy and charisma are priceless. Getting mood and knowledge :)

  • @roflox420
    @roflox420 5 лет назад +2

    This series is super. I have been working on a project that is using these things but I didn't know how some parts of this project work. Awesome! :)

  • @diegorobayo9013
    @diegorobayo9013 4 года назад +3

    This is the tutorial I have been looking for. Thanks a lot! Love everything about the tutorial. Including the new haircut!

  • @Blue-bb9ro
    @Blue-bb9ro 2 года назад

    Just found out this channel, I could tell you're a GREAT teacher, thank you for this simple explanation!!!!!

  • @marshallbruce2917
    @marshallbruce2917 2 года назад

    after days of post tutorials finally i understood .thanks bro

  • @asenaakb
    @asenaakb 2 года назад +2

    Wow I did not know how much I needed this! You actually make learning fun! Thank you so much :) My classes are very dry and boring in comparison.

  • @michaelreynoso2289
    @michaelreynoso2289 2 года назад

    You are heaven sent, thank you! best node.js teacher on RUclips by far

  • @genesisebena188
    @genesisebena188 4 года назад +1

    Honnetly i dont speak english very weel and it the same to leastening somebody speaking quicly in english, but your video make me understand how node and api communicate. So thank u so much

  • @PerryCS2
    @PerryCS2 Год назад

    It's a beautiful day in the neighborhood, a beautiful day in the neighborhood, won't you be, my neighbor... sorry, love your tutorials but you reminded me of a childhood memory within the first 1 second of this video... LOL! had to comment (Mr Rogers if anyone remembers [Canadian TV show]). lol

  • @briandethloff2451
    @briandethloff2451 3 года назад

    Spent two hours trying to figure out why req.body was returning undefined before coming across this tutorial... Thanks for making it

  • @yt.mhasan
    @yt.mhasan 5 лет назад +1

    Every time I watch your video, I feel lucky for whatever reason!!!

  • @walimorris2406
    @walimorris2406 3 года назад

    Off the bat, I love your teaching style. Very clear.

  • @carlosluna6859
    @carlosluna6859 4 года назад +3

    your classes are so much fun! congrats on being so didactic

  • @technoworld3591
    @technoworld3591 4 года назад

    Dear you are great Teacher and your teaching style best for any student who just come to your channel.👏

  • @kristofvanherzeele5649
    @kristofvanherzeele5649 2 года назад

    Very nice video, I searched a long time for a video like this. Thanks!

  • @edwardcerverizzo7363
    @edwardcerverizzo7363 4 года назад +4

    I had trouble with the POST fetch request when using live server.
    If you are having the same problem open "localhost:3000" in the browser instead of using the tab that live server uses.

    • @dgh25
      @dgh25 Год назад

      THANKS!!!!!!!!!! It was making me insane - what a trooper!

  • @patrickaikonia853
    @patrickaikonia853 4 года назад +2

    I am a student from Uganda learning react Native. But I was looking for away to deal with API and now I have learnt thanks to "What does it mean to be a bubble?". Thanks

  • @yournerdiness3135
    @yournerdiness3135 Год назад +1

    I cannot thank you enough for this, it has helped me with my personal projects so much.

  • @IronX88
    @IronX88 4 года назад

    EXACTLY what i needed. Firefox didnt like how i passed the information through i guess. Works now thanks to you!

  • @TheJP100
    @TheJP100 2 года назад +1

    just what I was looking for, thank you for this video!

  • @calltoreason
    @calltoreason 4 года назад +3

    Quick question. I did every video step by step up to this point and i always get this error:
    POST 127.0.0.1:5500/api 405 (Method Not Allowed)
    Can someone help me about this? I am trying for a month now and I am hetting nowhere.
    I've downloaded your code as well and the error is still there. Please help!

    • @taekwonkey
      @taekwonkey 4 года назад

      I had the same problem and so I made a directory(folder) called "api" inside the same directory as index.js. It seems to have solved it.

    • @nsantomeba
      @nsantomeba Год назад

      Ok, I found the solution for the ERR_ABORTED 405 (Method Not Allowed)
      The error appears in the console of our index.html file when accessing the file DIRECTLY (in my case via VS Code - Live Server). By accessing directly to the file we somehow bypass the server logic or functionality.
      In order for the correct Server-Client functionality work we must enter the server via the URL "localhost:(whichever port its listening)" and as a result, the server will display the index.html and all the fetch functionalities should work. Cheers!

  • @joshuacarpentier2393
    @joshuacarpentier2393 2 года назад

    Great video every time i re-watch I pick a little more

  • @agled
    @agled 5 лет назад +13

    Man, you are awesome. I'm from Brazil and I always excited when youtube sugest to watch one of your videos. I have learning a lot! Thanks
    By the way, nice haircut. ;)

  • @mbdnc
    @mbdnc 5 лет назад +1

    I had some udemy courses on node but your videos have a different taste 🙂 I am glad you exist 😄 thanks for everything

  • @SteampunkSammy
    @SteampunkSammy 5 лет назад +36

    'API' instead of 'Rainbow'? my my.. that haircut has made you too sensible!!
    :) Great video as always

  • @truthShallPrevailRRVS
    @truthShallPrevailRRVS 4 года назад +1

    !!!!Important: I did all kinds of things and could never get body-parser or the new express.json() to work... And I finally figured out the problem was caused on Postman's side !
    I was sending a RAW payload and it was being sent as Text instead of JSON ! I changed that to JSON and everything worked 😊
    Hope this helps 😄

  • @danieloliveira9815
    @danieloliveira9815 3 года назад

    Uhuu, after 4 days of lazy work i finally finished the exercises that he proposed in the video :D Ty Daniel

  • @ChrisMochinskiMusic
    @ChrisMochinskiMusic 3 года назад

    I owe you a coffee for the help. This POST/GET things is killing me in my learning adventure!

  • @bongbuincyril6150
    @bongbuincyril6150 4 года назад

    The way u introduce node was so helpful for me thanks alot man

  • @darpanvadher8224
    @darpanvadher8224 3 года назад

    Thank you for USE this type of teaching technic. Love From INDIA

  • @PeterParker-mp7ns
    @PeterParker-mp7ns Год назад

    This is guy is so damn energetic. Makes things fun

  • @paulachena488
    @paulachena488 4 года назад

    I still didn't see the video. But that background already transmits me good vibes haha. Thank you!.

  • @rijuban4321
    @rijuban4321 3 года назад +2

    Followed the video step by step but im getting a 405 error saying method not allowed for my fetch api, any fix for this?

    • @Devilman91
      @Devilman91 3 года назад +1

      The same. Have you found a solution?

    • @nsantomeba
      @nsantomeba Год назад

      @@Devilman91 Ok, I found the solution for the ERR_ABORTED 405 (Method Not Allowed)
      The error appears in the console of our index.html file when accessing the file DIRECTLY (in my case via VS Code - Live Server). By accessing directly to the file we somehow bypass the server logic or functionality.
      In order for the correct Server-Client functionality work we must enter the server via the URL "localhost:(whichever port its listening)" and as a result, the server will display the index.html and all the fetch functionalities should work. Cheers!

  • @Ab-cj6gl
    @Ab-cj6gl 3 года назад

    this channel and the net ninja channel are just crazy filled with information 😍

  • @badhobby503
    @badhobby503 4 года назад +3

    Love the bell at the beginnings :D
    Great stuff as always!!

  • @anuroopov1
    @anuroopov1 10 месяцев назад

    Awesome tutorial, best teacher ... ever ...thanks a lot !!

  • @mitri-dvp
    @mitri-dvp 4 года назад

    man, those headers always get me. Thanks for the video!

  • @robertakid727
    @robertakid727 4 года назад

    They are great videos, I have learned a lot. I followed your instructions step by step but I couldn't save it to a database. spent the whole day again and again but no luck !

  • @leahbarnes5612
    @leahbarnes5612 4 года назад +1

    Really appreciate your approach and enthusiasm! Makes understanding this stuff easier, for sure. Great content and explanations. Thank you!!!

  • @candybro7825
    @candybro7825 3 года назад

    damn! i missed the express.json() in app.js and struggling all day why I get undefined in console .. u saved me now dude! Thanks a lot

  • @sudhanshoosarage3734
    @sudhanshoosarage3734 3 года назад

    That really made my day cleared all my queries.. Thanks Love from India

  • @kishorekumarsinghk6777
    @kishorekumarsinghk6777 3 года назад

    Your video helped me a lot in my college project works. Thanks a lot😉

  • @flebedev9974
    @flebedev9974 Год назад

    You make everything so understandable!

  • @siddharthchabukswar2
    @siddharthchabukswar2 2 года назад

    Great tutorial and explaination! Thanks! That space indentation makes me uncomfortable though.

  • @aishwaryam1052
    @aishwaryam1052 4 года назад

    Im searching for this from the past 3 hrs. Thank u so much

  • @leamucho
    @leamucho 9 месяцев назад

    great hair cut Dan, loved it! nice to see your quotidian jokes always!!

  • @billymcdavid8397
    @billymcdavid8397 3 года назад

    My day always better when I code

  • @ailtondevesse4738
    @ailtondevesse4738 4 года назад

    GOoooD, you saved me lol.
    Hello from Mozambique here! :D

  • @ivyhello
    @ivyhello 2 года назад

    I'm so so so grateful for this channel!!!

  • @bwtang
    @bwtang 4 года назад +3

    When i wrote fetch('/api', options); in my index.html file and refreshed the page, it said 405 Method Not Allowed. Is there something wrong in the server side that does not allow the method POST?

    • @sebastiaanvv
      @sebastiaanvv 4 года назад +2

      same thing here. Did you find what was wrong in your case?

    • @rijuban4321
      @rijuban4321 3 года назад

      Add another header in the object that you are posting using fetch...
      mode: no-cors. This should solve it

    • @nsantomeba
      @nsantomeba Год назад

      @@rijuban4321 it didn't work for me, but thanks for trying

    • @nsantomeba
      @nsantomeba Год назад

      Ok, I found the solution for the ERR_ABORTED 405 (Method Not Allowed)
      The error appears in the console of our index.html file when accessing the file DIRECTLY (in my case via VS Code - Live Server). By accessing directly to the file we somehow bypass the server logic or functionality.
      In order for the correct Server-Client functionality work we must enter the server via the URL "localhost:(whichever port its listening)" and as a result, the server will display the index.html and all the fetch functionalities should work. Cheers!

    • @nsantomeba
      @nsantomeba Год назад

      @@sebastiaanvv Ok, I found the solution for the ERR_ABORTED 405 (Method Not Allowed)
      The error appears in the console of our index.html file when accessing the file DIRECTLY (in my case via VS Code - Live Server). By accessing directly to the file we somehow bypass the server logic or functionality.
      In order for the correct Server-Client functionality work we must enter the server via the URL "localhost:(whichever port its listening)" and as a result, the server will display the index.html and all the fetch functionalities should work. Cheers!

  • @dilanbarham3121
    @dilanbarham3121 Год назад

    Thank you. you saved my life.

  • @Ashish123
    @Ashish123 3 года назад

    finally i found this after thank u so much I was very confused how fetch works

  • @srivatsascs
    @srivatsascs 4 года назад +1

    Thank you so much, this is what I was looking for and of course you had a video on it.

  • @shaun_rambaran
    @shaun_rambaran 5 лет назад +1

    Jeez Louise, and I thought XMLHttpRequest was confusing!
    This one's going to take some digesting.

  • @adnanis-haq7462
    @adnanis-haq7462 3 года назад

    where have you been all day of my life????????????.. .I've been looking for contents like this all my life, this on at 7:00

  • @danishiiui1585
    @danishiiui1585 4 года назад +2

    is fetch() not valid because when i use it.It give me error "this method is not allowed"?
    If someone know please reply

    • @adeelahmed9632
      @adeelahmed9632 4 года назад +1

      I am facing the same issue. **

    • @timefly6804
      @timefly6804 4 года назад

      @@adeelahmed9632 me too. help guys~~

    • @danishiiui1585
      @danishiiui1585 4 года назад

      ​@@adeelahmed9632 un install the node LTS version if you install and then install the current version of node the it will work fine

    • @danishiiui1585
      @danishiiui1585 4 года назад

      @@timefly6804 uninstall the node LTS version if you install and then install the current version of node the it will work fine

  • @BerndBausch
    @BerndBausch 5 лет назад +3

    Coding Train is great, but I keep wondering why Dan never runs into any CORS problems. Half of the fetch() calls I am making result in "Cross-Origin Request Blocked" errors. Extremely frustrating, as the documentation about CORS I have found so far is written in languages I don't understand, probably Sumerian or Etruscan.

    • @TheCodingTrain
      @TheCodingTrain  5 лет назад +1

      In all of my examples here I am making the request to the server hosting the code, so there are no CORS errors. I should DEFINITELY address how this works more in future videos!

    • @BerndBausch
      @BerndBausch 5 лет назад

      @@TheCodingTrain Thanks Daniel. I think I have figured it out by now, like a grown-up, but it is indeed confusing for a newbie. Especially when I get a same-origin error when innocently fetching something from the local filesystem (video 1.1).

  • @asjn3e
    @asjn3e 3 года назад

    thank you very much you fixed my problem

  • @kolyxix
    @kolyxix 4 года назад

    It is always a pleasure to watch your video. I appreciate the fun energy and vibe you bring to teaching. You should look into paid teaching platform like udacity and udemy.

  • @chrischen2103
    @chrischen2103 4 года назад

    very good tutorial and just subscribed. Looking forward to your machine learning courses :)

  • @himanshudewangan5285
    @himanshudewangan5285 2 года назад

    Thankyou so much it is very helpful🎉

  • @hellmorgan8924
    @hellmorgan8924 4 месяца назад

    You are GOATED man, Thanks

  • @darkagent9457
    @darkagent9457 2 года назад

    That was insanely helpful...

  • @MatthewShowman
    @MatthewShowman 3 года назад +2

    Really don't understand why I just get empty brackets at 9:55. Body is populating in options on the client side, but it's not being set to the server side.

  • @lu33312
    @lu33312 2 года назад

    You’re amazing at teaching 👏👏👏

  • @matt112fly
    @matt112fly 2 года назад

    amazing video man, its exactly what i was looking for 👍❤️

  • @fede-tk3rm
    @fede-tk3rm 3 года назад

    This video saved my life. Thank you so much :')

  • @mridulmittal2064
    @mridulmittal2064 2 года назад +2

    thanks a lot sir

  • @andrewmasters6005
    @andrewmasters6005 5 лет назад +5

    I'm having a small issue. I can't get the data to transfer to the next page and I don't know how to fix it. My body is just constantly empty.

    • @amrantomer8005
      @amrantomer8005 4 года назад

      Mine too, have you found what causing the problem?
      Also 'app.post' is flaged as "unresolved" in my Webstorm, I'm not sure why.

    • @vedvardhangyanmote8079
      @vedvardhangyanmote8079 4 года назад

      Yeah same here... if anyone of you solved this problem, then please send the solution

    • @tdsimpson
      @tdsimpson 4 года назад

      I had the same issue. It was a problem with my header.

    • @eugeniomonforte6366
      @eugeniomonforte6366 3 года назад

      @@tdsimpson Me too, I write 'header' instead of 'headerS'

  • @russellabraham9208
    @russellabraham9208 4 года назад

    Thanks. I like the FormData constructor and good old XMLHttpRequest for post, I should try fetch.

  • @nachtschwarmer63
    @nachtschwarmer63 4 года назад +3

    Thank you for this great tutorial! I am a complete newbie on backend work and this really gets me started!

    • @woutcorijn4079
      @woutcorijn4079 3 года назад +1

      I'm a bit to late, but i figured it out.
      you need to change the fetch to this.
      fetch("/api", options).then(response => response.json()).then(response =>
      console.log(response)
      );

  • @jackeylu2926
    @jackeylu2926 3 года назад

    This guy is amazing!! Great video!!

  • @nassav3
    @nassav3 5 лет назад +1

    Awesome haircut, awesome video! :)

  • @user-ef3fg3ps9y
    @user-ef3fg3ps9y 5 лет назад +1

    You are the best! Please, can you tell when you can post next step of this beautiful course?

    • @TheCodingTrain
      @TheCodingTrain  5 лет назад +1

      I'm posting about one video per day now for this course! If you subscribe and click the alarm bell you'll get a notification.

  • @carlosspark2756
    @carlosspark2756 3 года назад +1

    "fetch API cannot load file:///api. URL scheme "file" is not supported." why am i getting this error and how can i solve it?

  • @Nachio1978
    @Nachio1978 10 месяцев назад

    No se inglés y me entró de poco, pero están geniales los videos

  • @beersforever
    @beersforever 5 лет назад +3

    Hey, I've got a quick question.
    When we first console.log'd the request, we got all of the data and it worked well, but when we specified request.body it didn't work anymore?
    You said that we needed to add the ability to parse any incoming data. How come the server was able to parse everything, but needs extra syntax when we want something more specific?
    Thank you for the great videos, these are really helping me out. I love your personality.

    • @SandFoxling
      @SandFoxling 5 лет назад

      I was wondering the exact same thing... considering all that data was also in json format

    • @amitrai420
      @amitrai420 3 года назад

      Server did not parse the initial request data it just printed out whatever it received which was already in json format from client side but when we said request.body server had to parse it.