Golang Gofiber BodyParser receive Unstructured JSON Array in REST Post API

Viewed 36

I have a REST API that sends me the following body

{
  "location": [
    {
      "activity": {
        "confidence": 100,
        "type": "unknown"
      },
      "battery": {
        "is_charging": false,
        "level": -1
      },
      "coords": {
        "accuracy": 5,
        "altitude": 0,
        "altitude_accuracy": -1,
        "floor": null,
        "heading": -1,
        "heading_accuracy": -1,
        "latitude": 3.162092,
        "longitude": 101.627155,
        "speed": -1,
        "speed_accuracy": -1
      },
      "extras": {},
      "is_moving": false,
      "mock": true,
      "odometer": 0,
      "timestamp": "2022-09-14T06:40:35.932Z",
      "uuid": "69778BD5-D618-48B7-A53D-EC37B67ED693"
    },
    {
      "activity": {
        "confidence": 100,
        "type": "unknown"
      },
      "battery": {
        "is_charging": false,
        "level": -1
      },
      "coords": {
        "accuracy": 5,
        "altitude": 0,
        "altitude_accuracy": -1,
        "floor": null,
        "heading": -1,
        "heading_accuracy": -1,
        "latitude": 3.162092,
        "longitude": 101.627155,
        "speed": -1,
        "speed_accuracy": -1
      },
      "extras": {},
      "is_moving": false,
      "mock": true,
      "odometer": 0,
      "timestamp": "2022-09-14T06:40:42.308Z",
      "uuid": "7970FFA3-F96C-49BC-81DB-37591556C3B3"
    }
  ]
}

Now in my code, i have written the following code

package apisLocation

import (
    "fmt"
    "time"
    "github.com/gofiber/fiber/v2"
    "gorm.io/gorm"
)

type LocationHistory struct {
    gorm.Model
    UserId        int       `json:"user_id"`
    Speed             float64   `json:"speed"`
    Altitude          float64   `json:"altitude"`
    Heading           float64   `json:"heading"`
    DeviceId          string    `json:"device_id"`
    LocationTrackedAt time.Time `json:"location_tracked_at"`
}

type Activity struct {
    Confidence int    `json:"confidence"`
    Type       string `json:"type"`
}

type Battery struct {
    IsCharging bool `json:"is_charging"`
    Level      int  `json:"level"`
}

type Coords struct {
    Accuracy         int     `json:"accuracy"`
    Altitude         int     `json:"altitude"`
    AltitudeAccuracy int     `json:"altitude_accuracy"`
    Floor            *int    `json:"floor"`
    Heading          int     `json:"heading"`
    HeadingAccuracy  int     `json:"heading_accuracy"`
    Latitude         float64 `json:"latitude"`
    Longitude        float64 `json:"longitude"`
    Speed            int     `json:"speed"`
    SpeedAccuracy    int     `json:"speed_accuracy"`
}

type Locations struct {
    Activity  Activity  `json:"activity"`
    Battery   Battery   `json:"battery"`
    Coords    Coords    `json:"coords"`
    Extras    struct{}  `json:"extras,omitempty"`
    IsMoving  bool      `json:"is_moving"`
    Mock      bool      `json:"mock"`
    Odometer  int       `json:"odometer"`
    Timestamp time.Time `json:"timestamp"`
    Uuid      string    `json:"uuid"`
}


func CreateLocation(c *fiber.Ctx) error {
    var payload []Locations

    if err := c.BodyParser(&payload); err != nil {
        return err
    }

    
    return c.Status(200).JSON(fiber.Map{"message": "OK"})
}

But this doesn't seem to work as i get the error json: cannot unmarshal object into Go value of type []apisLocation.Locations when i try to receive or return the payload in my code.

I have also tried just defining my payload as the following but all efforts were unsuccessful as well

payload := struct {
        Location map[string]interface{}
    }{} 


var payload []struct{}

Given an unstructured request body in Golang, how can i receive the request body using Gofiber BodyParser

Any assistance is appreciated.

0 Answers
Related