{"id":315604,"date":"2020-12-27T09:00:20","date_gmt":"2020-12-27T09:00:20","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=315604"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2020-12-27T09:00:20","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=315604","title":{"rendered":"\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 API \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Golang"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<blockquote><p>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0434\u043b\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c API \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e G\u043e.<\/p><\/blockquote>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/vm\/jr\/ml\/vmjrmlapzmu-ojrk8n350j1nr1q.jpeg\"><\/p>\n<h3>\u0422\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f<\/h3>\n<p>  \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c Go \u043d\u0430 \u0432\u0430\u0448 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440.<\/p>\n<h3>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0440\u0430\u0431\u043e\u0442\u0435<\/h3>\n<p>  \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442. \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u0441 \u0438\u043c\u0435\u043d\u0435\u043c <b>main.go<\/b> \u0438 \u0432\u0432\u0435\u0434\u0438\u0442\u0435 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434:<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code class=\"bash\">package main  import (   &quot;fmt&quot;   &quot;net\/http&quot;   &quot;github.com\/gin-gonic\/gin&quot; )<\/code><\/pre>\n<p>  <\/p>\n<h4>\u041c\u0435\u0442\u043e\u0434\u044b POST \u0438 GET<\/h4>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b GET \u0438 POST, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c <b>GetMethod<\/b> \u0438 <b>PostMethod<\/b>:<\/p>\n<pre><code class=\"go\">func PostMethod(c *gin.Context) {   fmt.Println(&quot;\\napi.go 'PostMethod' called&quot;)   message := &quot;PostMethod called&quot;   c.JSON(http.StatusOK, message) } func GetMethod(c *gin.Context) {   fmt.Println(&quot;\\napi.go 'GetMethod' called&quot;)   message := &quot;GetMethod called&quot;   c.JSON(http.StatusOK, message) }<\/code><\/pre>\n<p>  <\/p>\n<h4>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f<\/h4>\n<p>  \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u043e\u0443\u0442\u0435\u0440 <b>Gin<\/b> \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432:<\/p>\n<pre><code class=\"bash\">func main() {   router := gin.Default() }<\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c <b>GetMethod<\/b> \u0438 <b>PostMethod<\/b>:<\/p>\n<pre><code class=\"bash\">func main() {   router := gin.Default()  router.POST(&quot;\/&quot;, PostMethod)   router.GET(&quot;\/&quot;, GetMethod)  }<\/code><\/pre>\n<p>  \u0418, \u0432 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0433\u043e, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u0440\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0438 \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f:<\/p>\n<pre><code class=\"go\">func main() {   router := gin.Default() router.POST(&quot;\/&quot;, PostMethod)   router.GET(&quot;\/&quot;, GetMethod) listenPort := &quot;4000&quot; router.Run(&quot;:&quot;+listenPort) }<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043a\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440.<\/p>\n<h3>\u0410\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h3>\n<p>  \u041a\u043e\u0433\u0434\u0430 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d, \u043d\u0430\u0447\u043d\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f API \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 <b>func main()<\/b>:<\/p>\n<pre><code class=\"go\">func main() {   router := gin.Default() subRouterAuthenticated := router.Group(&quot;\/api\/v1\/PersonId&quot;, gin.BasicAuth(gin.Accounts{     &quot;admin&quot;: &quot;adminpass&quot;,   })) listenPort := &quot;1357&quot; router.Run(&quot;:&quot;+listenPort) }<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0439\u0442\u0435 query-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:<\/p>\n<pre><code class=\"go\">func main() {   router := gin.Default() subRouterAuthenticated := router.Group(&quot;\/api\/v1\/PersonId&quot;, gin.BasicAuth(gin.Accounts{     &quot;admin&quot;: &quot;adminpass&quot;,   })) subRouterAuthenticated.GET(&quot;\/:IdValue&quot;, GetMethod) listenPort := &quot;1357&quot;   router.Run(&quot;:&quot;+listenPort) }<\/code><\/pre>\n<p>  <\/p>\n<h4>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 GetMethod()<\/h4>\n<p>  <b>GetMethod()<\/b> \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 <b>Person IdValue<\/b> \u0438\u0437 query-\u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0432 URL-\u0430\u0434\u0440\u0435\u0441\u0435 API:<\/p>\n<pre><code class=\"go\">func GetMethod(c *gin.Context) {   fmt.Println(&quot;\\n'GetMethod' called&quot;)   IdValue := c.Params.ByName(&quot;IdValue&quot;)   message := &quot;GetMethod Called With Param: &quot; + IdValue   c.JSON(http.StatusOK, message) ReqPayload := make([]byte, 1024)   ReqPayload, err := c.GetRawData()   if err != nil {         fmt.Println(err)         return   }   fmt.Println(&quot;Request Payload Data: &quot;, string(ReqPayload)) }<\/code><\/pre>\n<p>  <b>\u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/b> <\/p>\n<pre><code class=\"go\">package main import (   &quot;fmt&quot;   &quot;net\/http&quot;   &quot;github.com\/gin-gonic\/gin&quot; ) func GetMethod(c *gin.Context) {   fmt.Println(&quot;\\n'GetMethod' called&quot;)   IdValue := c.Params.ByName(&quot;IdValue&quot;)   message := &quot;GetMethod Called With Param: &quot; + IdValue   c.JSON(http.StatusOK, message) ReqPayload := make([]byte, 1024)   ReqPayload, err := c.GetRawData()   if err != nil {         fmt.Println(err)         return   }   fmt.Println(&quot;Request Payload Data: &quot;, string(ReqPayload)) } func main() {   router := gin.Default() subRouterAuthenticated := router.Group(&quot;\/api\/v1\/PersonId&quot;, gin.BasicAuth(gin.Accounts{     &quot;admin&quot;: &quot;adminpass&quot;,   })) subRouterAuthenticated.GET(&quot;\/:IdValue&quot;, GetMethod) listenPort := &quot;1357&quot;      router.Run(&quot;:&quot;+listenPort) }<\/code><\/pre>\n<p>  <\/p>\n<h3>\u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ngrok<\/h3>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 Go:<\/p>\n<pre><code class=\"bash\">go run main.go<\/code><\/pre>\n<p>  1. \u0421\u043a\u0430\u0447\u0430\u0439\u0442\u0435 <a href=\"https:\/\/dl.equinox.io\/ngrok\/ngrok\/stable\">ngrok<\/a>.<\/p>\n<p>  2. \u0420\u0430\u0441\u043f\u0430\u043a\u0443\u0439\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u0444\u0430\u0439\u043b ngrok \u0432 \u043f\u0430\u043f\u043a\u0443 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435.<\/p>\n<p>  3. \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 ngrok \u043d\u0430 \u043f\u043e\u0440\u0442\u0443 1357 (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u0432 \u043a\u043e\u0434\u0435):<\/p>\n<pre><code class=\"bash\">.\/ngrok http 1357<\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <code>ngrok by @emmyc (Ctrl+C to quit)<br \/>  Session Status online <br \/>  Session Expires 7 hours, 12 minutes <br \/>  Version 2.3.35 <br \/>  Region Netherlands (nl) <br \/>  Web Interface http:\/\/127.0.0.1:4040 <br \/>  Forwarding http:\/\/ca6d2c4cee3e.ngrok.io -&gt; http:\/\/localhost:4000 <br \/>  Forwarding https:\/\/ca6d2c4cee3e.ngrok.io -&gt; http:\/\/localhost:4000<\/code><\/p>\n<p>  \u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 URL-\u0430\u0434\u0440\u0435\u0441, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 API \u0438 \u0432\u043e\u0439\u0442\u0438 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0441\u043e \u0441\u0432\u043e\u0438\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0432\u0441\u0435 \u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0432\u0445\u043e\u0434\u0430 \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435:<\/p>\n<pre><code class=\"bash\">GetMethod Called With Param: Id456<\/code><\/pre>\n<p>  \u0412\u0441\u0451 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442!<\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u043c \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 Go API \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <b>Gin<\/b>. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u044b \u0443\u0437\u043d\u0430\u043b\u0438, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438 \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ngrok.<\/p>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u044d\u0442\u0438\u0445 \u0437\u043d\u0430\u043d\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 \u043d\u0430 Go.<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/timeweb\/blog\/535178\/\"> https:\/\/habr.com\/ru\/company\/timeweb\/blog\/535178\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<blockquote><p>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0434\u043b\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c API \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e G\u043e.<\/p><\/blockquote>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/vm\/jr\/ml\/vmjrmlapzmu-ojrk8n350j1nr1q.jpeg\"><\/p>\n<h3>\u0422\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f<\/h3>\n<p>  \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c Go \u043d\u0430 \u0432\u0430\u0448 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440.<\/p>\n<h3>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u0440\u0430\u0431\u043e\u0442\u0435<\/h3>\n<p>  \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442. \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u044b\u0439 \u0444\u0430\u0439\u043b \u0441 \u0438\u043c\u0435\u043d\u0435\u043c <b>main.go<\/b> \u0438 \u0432\u0432\u0435\u0434\u0438\u0442\u0435 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0439 \u043a\u043e\u0434:  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-315604","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/315604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=315604"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/315604\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=315604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=315604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=315604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}