dsfs5420 2019-04-01 11:58 采纳率: 100%
浏览 102
已采纳

API请求和响应

I would like to find out if it is possible for an HTTP client to POST a specific JSON object and server response with a different JSON object in Go. For a example a client send the JSON object

request body

{
  "nfNssaiAvailabilityUri": "string",
  "taiList": [
    {
      "plmnId": {
        "mcc": "string",
        "mnc": "string"
      },
      "tac": "string"
    }
  ],
  "expiry": "2019-04-01T10:41:54.344Z"
}

response body as

{
  "subscriptionId": "string",
  "expiry": "2019-04-01T10:41:54.363Z",
  "authorizedNssaiAvailabilityData": [
    {
      "tai": {
        "plmnId": {
          "mcc": "string",
          "mnc": "string"
        },
        "tac": "string"
      },
      "supportedSnssaiList": [
        {
          "sst": 0,
          "sd": "string"
        }
      ],
      "restrictedSnssaiList": [
        {
          "homePlmnId": {
            "mcc": "string",
            "mnc": "string"
          },
          "sNssaiList": [
            {
              "sst": 0,
              "sd": "string"
            }
          ]
        }
      ]
    }
  ]
}

As you can see, the request body JSON is different from the response. I have structs for both JSONs and currently I can only POST with the request body and receive the same body as response. What am expecting is to be able to get a JSON object indicated in the above response.

I have:

type NssfEventSubscriptionCreateData struct {
    NfNssaiAvailabilityUri string `json:"nfNssaiAvailabilityUri"`
    TaiList []Tai `json:"taiList,omitempty"`
        ...
}
type NssfEventSubscriptionCreatedData struct {
    SubscriptionId string `json:"subscriptionId"`
    Expiry time.Time `json:"expiry,omitempty"`
        ....
}
func (m *SliceDataAccess) InsertNssaiSubscriptionInfo(subdata NssfEventSubscriptionCreateData) error {
    err := db.C(COLLECTION).Insert(subdata)
    if err != nil {
        return err
    }
    return nil
}
func NSSAIAvailabilityPost(w http.ResponseWriter, r *http.Request) {
    if r.Header.Get("Accept") != "application/json" {
        WriteError(w, ErrNotAcceptable)
        return
    }
    if r.Method == "POST" {

        var reqsubData NssfEventSubscriptionCreateData
        err := json.NewDecoder(r.Body).Decode(&reqsubData)
        if err != nil {
            respondWithError(w, http.StatusBadRequest, "Object body not well decoded")
            return
        }
        //reqsubData.ID = bson.NewObjectId()
        if err := da.InsertNssaiSubscriptionInfo(reqsubData); err != nil {
            respondWithError(w, http.StatusInternalServerError, err.Error())
        } else {
            scheme := "http"
            if r.URL.Scheme != "" {
                scheme = r.URL.Scheme
            }
            w.Header().Set("Location", scheme+"://"+r.Host+r.URL.Path)
            w.Header().Set("Response-Code", "201")
            w.Header().Set("Response-Desc", "Success")
            respondWithJson(w, http.StatusCreated, reqsubData)
        }

    }
}

The NSSAIAvailabilityPost function respond with NssfEventSubscriptionCreateData struct type JSON object but I would like to be able to respond with the NssfEventSubscriptionCreatedData struct type JSON object.

  • 写回答

1条回答 默认 最新

  • dongzhuo3376 2019-04-01 12:04
    关注

    Create a struct of type NssfEventSubscriptionCreatedData initialise its values and return in respondWithJSON.

    respData : = NssfEventSubscriptionCreatedData{}
    // init fields
    
     respondWithJson(w, http.StatusCreated, respData)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法