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.