foomo-docs/assets/js/388fbbd1.20590cef.js
2023-04-05 08:53:53 +00:00

1 line
6.9 KiB
JavaScript

"use strict";(self.webpackChunkfoomo=self.webpackChunkfoomo||[]).push([[8988],{3905:(e,t,n)=>{n.d(t,{Zo:()=>c,kt:()=>h});var r=n(7294);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function s(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){a(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function l(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var i=r.createContext({}),p=function(e){var t=r.useContext(i),n=t;return e&&(n="function"==typeof e?e(t):s(s({},t),e)),n},c=function(e){var t=p(e.components);return r.createElement(i.Provider,{value:t},e.children)},d="mdxType",u={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},m=r.forwardRef((function(e,t){var n=e.components,a=e.mdxType,o=e.originalType,i=e.parentName,c=l(e,["components","mdxType","originalType","parentName"]),d=p(n),m=a,h=d["".concat(i,".").concat(m)]||d[m]||u[m]||o;return n?r.createElement(h,s(s({ref:t},c),{},{components:n})):r.createElement(h,s({ref:t},c))}));function h(e,t){var n=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var o=n.length,s=new Array(o);s[0]=m;var l={};for(var i in t)hasOwnProperty.call(t,i)&&(l[i]=t[i]);l.originalType=e,l[d]="string"==typeof e?e:a,s[1]=l;for(var p=2;p<o;p++)s[p]=n[p];return r.createElement.apply(null,s)}return r.createElement.apply(null,n)}m.displayName="MDXCreateElement"},4099:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>i,contentTitle:()=>s,default:()=>u,frontMatter:()=>o,metadata:()=>l,toc:()=>p});var r=n(7462),a=(n(7294),n(3905));const o={Title:"HTTP Client and Server",position:12,tags:["Go Basics"]},s="HTTP Client and Server",l={unversionedId:"backend/go-by-example/http",id:"backend/go-by-example/http",title:"HTTP Client and Server",description:"The HTTP protocol is used to communicate between Client and Server. The `net/http` package provides all methods needed to implement a client and",source:"@site/docs/backend/go-by-example/http.mdx",sourceDirName:"backend/go-by-example",slug:"/backend/go-by-example/http",permalink:"/docs/backend/go-by-example/http",draft:!1,editUrl:"https://github.com/foomo/foomo-docs/tree/main/foomo/docs/backend/go-by-example/http.mdx",tags:[{label:"Go Basics",permalink:"/docs/tags/go-basics"}],version:"current",frontMatter:{Title:"HTTP Client and Server",position:12,tags:["Go Basics"]},sidebar:"backendSidebar",previous:{title:"Go Routines and Channels",permalink:"/docs/backend/go-by-example/goroutines-and-channels"},next:{title:"Interfaces",permalink:"/docs/backend/go-by-example/interfaces"}},i={},p=[{value:"HTTP Client",id:"http-client",level:2},{value:"HTTP Server",id:"http-server",level:2},{value:"RESTful",id:"restful",level:3}],c={toc:p},d="wrapper";function u(e){let{components:t,...n}=e;return(0,a.kt)(d,(0,r.Z)({},c,n,{components:t,mdxType:"MDXLayout"}),(0,a.kt)("h1",{id:"http-client-and-server"},"HTTP Client and Server"),(0,a.kt)("p",null,"The HTTP protocol is used to communicate between Client and Server. The ",(0,a.kt)("inlineCode",{parentName:"p"},"net/http")," package provides all methods needed to implement a client and\na server and the most commonly used request methods."),(0,a.kt)("p",null,"The most commonly used request methods are:"),(0,a.kt)("ul",null,(0,a.kt)("li",{parentName:"ul"},"GET:",(0,a.kt)("ul",{parentName:"li"},(0,a.kt)("li",{parentName:"ul"},"This method is used to request data from the server or specified source. Used for fetching data."))),(0,a.kt)("li",{parentName:"ul"},"POST:",(0,a.kt)("ul",{parentName:"li"},(0,a.kt)("li",{parentName:"ul"},"This method is used to send a request with data in the body to a server. Used for creating data."))),(0,a.kt)("li",{parentName:"ul"},"PATCH:",(0,a.kt)("ul",{parentName:"li"},(0,a.kt)("li",{parentName:"ul"},"This method is used to partially update an existing data entry."))),(0,a.kt)("li",{parentName:"ul"},"PUT:",(0,a.kt)("ul",{parentName:"li"},(0,a.kt)("li",{parentName:"ul"},"This method is used to entirely update an existing data entry.")))),(0,a.kt)("h2",{id:"http-client"},"HTTP Client"),(0,a.kt)("p",null,"The HTTP Client consumes the API, it sends a request to the HTTP Server. The following code shows an example for all the commonly used HTTP requests."),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-go"},'\n// GET Requests: Pass the URL to request the data from.\n// Returns a response and an error.\nresp, err := http.Get("https://foomo.org") \n\n// Post Requests: Pass the URL to post data to, specify the content type of the data, \n// and pass the body containing the data of the request.\n// Returns a response and an error.\nresp, err := http.Post("https://foomo.org", "application/json", body)\n\n// PATCH Requests, specify it\'s a PATCH request, pass the URL to request, \n// and the body containing the data of the request.\n// Returns a response and an error.\nresp, err := http.NewRequest(http.MethodPatch, "https://foomo.org", body) \n\n// PUT Requests, specify it\'s a PUT request, pass the URL to request,\n// and the body containing the data of the request.\n// Returns a response and an error.\nresp, err := http.NewRequest(http.MethodPut, "https://foomo.org", body) \n\n')),(0,a.kt)("h2",{id:"http-server"},"HTTP Server"),(0,a.kt)("p",null,"the HTTP Server accepts API calls from a Client. Handlers are a fundamental concept in the ",(0,a.kt)("inlineCode",{parentName:"p"},"net/http")," package.\nThey are your way of basically telling how a request is supposed to be handled. Handler functions always take a ResponseWriter and a pointer to a Request as argument:"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-go"},"func myHandler(w http.ResponseWriter, req *http.Request) {\n ...\n}\n")),(0,a.kt)("p",null,"In the main function of your server you register all the handlers implemented and serve them on a specified port:"),(0,a.kt)("pre",null,(0,a.kt)("code",{parentName:"pre",className:"language-go"},'func main () {\n\n http.HandleFunc("/myPage", myHandler) // specify the path and the handler function\n\n http.ListenAndServe(":8000", nil) // specify the port and the router, which is the default router in this case.\n}\n')),(0,a.kt)("h3",{id:"restful"},"RESTful"))}u.isMDXComponent=!0}}]);