github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/http/README.md (about)

     1  <!-- Code generated by gomarkdoc. DO NOT EDIT -->
     2  
     3  # http
     4  
     5  ```go
     6  import "github.com/cloudwego/dynamicgo/http"
     7  ```
     8  
     9  ## Index
    10  
    11  - [Constants](<#constants>)
    12  - [Variables](<#variables>)
    13  - [func AnnoToMethod(annoKey string) string](<#func-annotomethod>)
    14  - [type Endpoint](<#type-endpoint>)
    15  - [type HTTPRequest](<#type-httprequest>)
    16    - [func NewHTTPRequest() *HTTPRequest](<#func-newhttprequest>)
    17    - [func NewHTTPRequestFromStdReq(req *http.Request, params ...Param) (ret *HTTPRequest, err error)](<#func-newhttprequestfromstdreq>)
    18    - [func NewHTTPRequestFromUrl(method, url string, body io.Reader, params ...Param) (*HTTPRequest, error)](<#func-newhttprequestfromurl>)
    19    - [func (self HTTPRequest) Body() []byte](<#func-httprequest-body>)
    20    - [func (self HTTPRequest) Cookie(key string) string](<#func-httprequest-cookie>)
    21    - [func (self HTTPRequest) Header(key string) string](<#func-httprequest-header>)
    22    - [func (self HTTPRequest) Host() string](<#func-httprequest-host>)
    23    - [func (self *HTTPRequest) MapBody(key string) string](<#func-httprequest-mapbody>)
    24    - [func (self HTTPRequest) Method() string](<#func-httprequest-method>)
    25    - [func (self HTTPRequest) Param(key string) string](<#func-httprequest-param>)
    26    - [func (self HTTPRequest) Path() string](<#func-httprequest-path>)
    27    - [func (self HTTPRequest) PostForm(key string) string](<#func-httprequest-postform>)
    28    - [func (self HTTPRequest) Query(key string) string](<#func-httprequest-query>)
    29    - [func (self HTTPRequest) Uri() string](<#func-httprequest-uri>)
    30  - [type HTTPResponse](<#type-httpresponse>)
    31    - [func NewHTTPResponse() *HTTPResponse](<#func-newhttpresponse>)
    32    - [func (self HTTPResponse) SetCookie(key string, val string) error](<#func-httpresponse-setcookie>)
    33    - [func (self HTTPResponse) SetHeader(key string, val string) error](<#func-httpresponse-setheader>)
    34    - [func (self HTTPResponse) SetRawBody(body []byte) error](<#func-httpresponse-setrawbody>)
    35    - [func (self HTTPResponse) SetStatusCode(code int) error](<#func-httpresponse-setstatuscode>)
    36  - [type Param](<#type-param>)
    37  - [type Params](<#type-params>)
    38    - [func (ps *Params) ByName(name string) string](<#func-params-byname>)
    39    - [func (ps *Params) Recycle()](<#func-params-recycle>)
    40    - [func (ps *Params) Set(name string, val string) bool](<#func-params-set>)
    41  - [type RequestGetter](<#type-requestgetter>)
    42  - [type ResponseSetter](<#type-responsesetter>)
    43  
    44  
    45  ## Constants
    46  
    47  ```go
    48  const (
    49      // HeaderContentType is the key of Content-Type header
    50      HeaderContentType = "Content-Type"
    51      // HeaderSetCookie is the key of Set-Cookie header
    52      HeaderSetCookie = "Set-Cookie"
    53  )
    54  ```
    55  
    56  ## Variables
    57  
    58  ```go
    59  var (
    60      // DefaultJsonPairSize is the default size of json.Pair slice.
    61      DefaultJsonPairSize = 16
    62  )
    63  ```
    64  
    65  ## func AnnoToMethod
    66  
    67  ```go
    68  func AnnoToMethod(annoKey string) string
    69  ```
    70  
    71  AnnoToMethod maps annotation to corresponding http method
    72  
    73  ## type Endpoint
    74  
    75  Endpoint a http endpoint.
    76  
    77  ```go
    78  type Endpoint struct {
    79      Method, Path string
    80  }
    81  ```
    82  
    83  ## type HTTPRequest
    84  
    85  Request is a implementation of RequestGetter. It wraps http.Request.
    86  
    87  ```go
    88  type HTTPRequest struct {
    89      *http.Request
    90  
    91      Params  Params
    92      BodyMap interface{}
    93      // contains filtered or unexported fields
    94  }
    95  ```
    96  
    97  ### func NewHTTPRequest
    98  
    99  ```go
   100  func NewHTTPRequest() *HTTPRequest
   101  ```
   102  
   103  NewHTTPRequest creates a new HTTPRequest.
   104  
   105  ### func NewHTTPRequestFromStdReq
   106  
   107  ```go
   108  func NewHTTPRequestFromStdReq(req *http.Request, params ...Param) (ret *HTTPRequest, err error)
   109  ```
   110  
   111  NewHTTPRequestFromStdReq creates a new HTTPRequest from http.Request. It will check the content\-type of the request and parse the body if the type one of following: \- application/json \- application/x\-www\-form\-urlencoded
   112  
   113  ### func NewHTTPRequestFromUrl
   114  
   115  ```go
   116  func NewHTTPRequestFromUrl(method, url string, body io.Reader, params ...Param) (*HTTPRequest, error)
   117  ```
   118  
   119  NewHTTPRequestFromUrl creates a new HTTPRequest from url, body and url\-path param.
   120  
   121  ### func \(HTTPRequest\) Body
   122  
   123  ```go
   124  func (self HTTPRequest) Body() []byte
   125  ```
   126  
   127  Body implements RequestGetter.Body.
   128  
   129  ### func \(HTTPRequest\) Cookie
   130  
   131  ```go
   132  func (self HTTPRequest) Cookie(key string) string
   133  ```
   134  
   135  Cookie implements RequestGetter.Cookie.
   136  
   137  ### func \(HTTPRequest\) Header
   138  
   139  ```go
   140  func (self HTTPRequest) Header(key string) string
   141  ```
   142  
   143  Header implements RequestGetter.Header.
   144  
   145  ### func \(HTTPRequest\) Host
   146  
   147  ```go
   148  func (self HTTPRequest) Host() string
   149  ```
   150  
   151  Host implements RequestGetter.Host.
   152  
   153  ### func \(\*HTTPRequest\) MapBody
   154  
   155  ```go
   156  func (self *HTTPRequest) MapBody(key string) string
   157  ```
   158  
   159  MapBody implements RequestGetter.MapBody.
   160  
   161  ### func \(HTTPRequest\) Method
   162  
   163  ```go
   164  func (self HTTPRequest) Method() string
   165  ```
   166  
   167  Method implements RequestGetter.Method.
   168  
   169  ### func \(HTTPRequest\) Param
   170  
   171  ```go
   172  func (self HTTPRequest) Param(key string) string
   173  ```
   174  
   175  Param implements RequestGetter.Param.
   176  
   177  ### func \(HTTPRequest\) Path
   178  
   179  ```go
   180  func (self HTTPRequest) Path() string
   181  ```
   182  
   183  Path implements RequestGetter.Path.
   184  
   185  ### func \(HTTPRequest\) PostForm
   186  
   187  ```go
   188  func (self HTTPRequest) PostForm(key string) string
   189  ```
   190  
   191  PostForm implements RequestGetter.PostForm.
   192  
   193  ### func \(HTTPRequest\) Query
   194  
   195  ```go
   196  func (self HTTPRequest) Query(key string) string
   197  ```
   198  
   199  Query implements RequestGetter.Query.
   200  
   201  ### func \(HTTPRequest\) Uri
   202  
   203  ```go
   204  func (self HTTPRequest) Uri() string
   205  ```
   206  
   207  Uri implements RequestGetter.Uri.
   208  
   209  ## type HTTPResponse
   210  
   211  HTTPResponse is an implementation of ResponseSetter
   212  
   213  ```go
   214  type HTTPResponse struct {
   215      *http.Response
   216  }
   217  ```
   218  
   219  ### func NewHTTPResponse
   220  
   221  ```go
   222  func NewHTTPResponse() *HTTPResponse
   223  ```
   224  
   225  NewHTTPResponse creates a new HTTPResponse
   226  
   227  ### func \(HTTPResponse\) SetCookie
   228  
   229  ```go
   230  func (self HTTPResponse) SetCookie(key string, val string) error
   231  ```
   232  
   233  SetCookie implements ResponseSetter.SetCookie
   234  
   235  ### func \(HTTPResponse\) SetHeader
   236  
   237  ```go
   238  func (self HTTPResponse) SetHeader(key string, val string) error
   239  ```
   240  
   241  SetHeader implements ResponseSetter.SetHeader
   242  
   243  ### func \(HTTPResponse\) SetRawBody
   244  
   245  ```go
   246  func (self HTTPResponse) SetRawBody(body []byte) error
   247  ```
   248  
   249  ### func \(HTTPResponse\) SetStatusCode
   250  
   251  ```go
   252  func (self HTTPResponse) SetStatusCode(code int) error
   253  ```
   254  
   255  SetStatusCode implements ResponseSetter.SetStatusCode
   256  
   257  ## type Param
   258  
   259  ### Param in url path
   260  
   261  e.g. /user/:id \+ /user/123 =\> Param\{Key: "id", Value: "123"\}
   262  
   263  ```go
   264  type Param struct {
   265      Key   string
   266      Value string
   267  }
   268  ```
   269  
   270  ## type Params
   271  
   272  Http url\-path params
   273  
   274  ```go
   275  type Params struct {
   276      // contains filtered or unexported fields
   277  }
   278  ```
   279  
   280  ### func \(\*Params\) ByName
   281  
   282  ```go
   283  func (ps *Params) ByName(name string) string
   284  ```
   285  
   286  ByName search Param by given name
   287  
   288  ### func \(\*Params\) Recycle
   289  
   290  ```go
   291  func (ps *Params) Recycle()
   292  ```
   293  
   294  Recycle the Params
   295  
   296  ### func \(\*Params\) Set
   297  
   298  ```go
   299  func (ps *Params) Set(name string, val string) bool
   300  ```
   301  
   302  Set set Param by given name and value, return true if Param exists
   303  
   304  ## type RequestGetter
   305  
   306  RequestGetter is a interface for getting request parameters
   307  
   308  ```go
   309  type RequestGetter interface {
   310      // Method returns the http method.
   311      Method() string
   312      // Host returns the host.
   313      Host() string
   314      // Uri returns entire uri.
   315      Uri() string
   316      // Header returns the value of the header with the given key.
   317      Header(string) string
   318      // Cookie returns the value of the cookie with the given key.
   319      Cookie(string) string
   320      // Query returns the value of the query with the given key.
   321      Query(string) string
   322      // Param returns the value of the url-path param with the given key.
   323      Param(string) string
   324      // PostForm returns the value of the post-form body with the given key.
   325      PostForm(string) string
   326      // MapBody returns the value of body with the given key.
   327      MapBody(string) string
   328      // Body returns the raw body in bytes.
   329      Body() []byte
   330  }
   331  ```
   332  
   333  ## type ResponseSetter
   334  
   335  ResponseSetter is a interface for setting response parameters
   336  
   337  ```go
   338  type ResponseSetter interface {
   339      // SetStatusCode sets the status code of the response
   340      SetStatusCode(int) error
   341      // SetHeader sets the header of the response
   342      SetHeader(string, string) error
   343      // SetCookie sets the cookie of the response
   344      SetCookie(string, string) error
   345      // SetRawBody sets the raw body of the response
   346      SetRawBody([]byte) error
   347  }
   348  ```
   349  
   350  
   351  
   352  Generated by [gomarkdoc](<https://github.com/princjef/gomarkdoc>)