Encountering an HTTP 422 response: Status Code:422 Unprocessable Entity
The console message from fmt.Println(c) is:
&{{0xc04227c1c0 -1 200} 0xc0421b2100 0xc042086d10 [] [0x8fdc00 0x8fe950 0x97e310 0x97cf80] 3 0xc0421ea5a0 map[] []}
Although the map should have values for myEmail
and myPassword
, it seems to be empty.
Is there an issue with the request body or does it relate to the web API?
Below is the HTTP request being made:
this.http.post('http://localhost:8080/api/v1/users', {'email': 'myEmail', 'password': 'myPassword'}, httpOptions)
.subscribe(data => {
console.log('register___', data);
});
These are the details of httpOptions being used:
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json'
, 'Access-Control-Allow-Origin': '*'
, 'Access-Control-Allow-Headers': 'access-control-allow-origin, access-control-allow-headers'})
};
This is the go web API that is being utilized:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
)
type Users struct {
email string `gorm:"not null" form:"email" json:"email"`
password string `gorm:"not null" form:"password" json:"password"`
}
func InitDb() *gorm.DB {
// Openning file
db, err := gorm.Open("sqlite3", "./data.db")
// Display SQL queries
db.LogMode(true)
// Error
if err != nil {
panic(err)
}
// Creating the table
if !db.HasTable(&Users{}) {
db.CreateTable(&Users{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&Users{})
}
return db
}
... (The rest of the code remains unchanged for brevity)