http

package
v0.0.0-...-88c2f3b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package http provides a set of functions for working with HTTP requests and responses. It is an extension of the go-zero package https://github.com/zeromicro/go-zero/tree/master/rest/httpx, it provides a base response struct, as before, you can only respond a json string like this:

httpx.OkJson(w, message{Name: "anyone"})

then you can receive a json string like this:

{"name":"anyone"}

but now, you can respond a json string with base response struct like this:

JsonBaseResponse(w, message{Name: "anyone"})

then you can receive a json string like this:

{"code":0,"msg":"ok","data":{"name":"anyone"}}

Index

Constants

View Source
const (
	// BusinessCodeOK represents the business code for success.
	BusinessCodeOK = 0
	// BusinessMsgOk represents the business message for success.
	BusinessMsgOk = "OK"

	// BusinessCodeError represents the business code for error.
	BusinessCodeError = -1

	BusinessMsgError = "INTERNAL SERVER ERROR"
)

Variables

This section is empty.

Functions

func Error

func Error(ctx context.Context, w http.ResponseWriter, v any)

func Success

func Success(ctx context.Context, w http.ResponseWriter, v any)

Types

type BaseResponse

type BaseResponse[T any] struct {
	// Code represents the business code, not the http status code.
	Code int `json:"code"`
	// Msg represents the business message, if Code = BusinessCodeOK,
	// and Msg is empty, then the Msg will be set to BusinessMsgOk.
	Msg string `json:"msg"`
	// Data represents the business data.
	Data T `json:"data,omitempty"`
}

BaseResponse is the base response struct.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL