Angular sent an HTTP request to GO, receiving a '422 Unprocessable Entity' status code in return

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)

Answer №1

To make the fields in your data structure accessible, you must export them by modifying the code as follows:

type Users struct {
    Email string `gorm:"not null" form:"email" json:"email"`
    Password  string `gorm:"not null" form:"password" json:"password"`
}

Currently, these fields are not exported and can only be accessed within your package. This restricts other packages from interacting with or manipulating the data through marshaling/unmarshaling operations.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that the ngOnChange is triggered after the BehaviorSubject emits a true value twice

I am currently developing an Angular 5 application. One of the components in my project is a dropdown component ( containing the following code in the html file, dropdown.component.html <p-dropdown [options]="data" [(ngModel)]="selectedFilterValue" pl ...

Unable to display Cell Properties in Angular using CKEditor 5 table toolbar configuration

While utilizing the CKEditor 5 official demo, I noticed that the table content Toolbar includes Cell properties. This feature is crucial for my needs, but despite configuring my table similarly to the demo setup, the Cell properties do not appear. The dem ...

Implementing routing for page navigation within an angular tree structure

Can someone assist me with integrating a route into an angular tree structure? Here is the HTML code snippet: <mat-tree [dataSource]="dataSource" class="tree-container" [treeControl]="treeControl"> <mat-tree-node class="btnLinks" *matTreeN ...

Parsing difficulties encountered: the element 'ion-col' is unrecognized

As I strive to develop Ionic 4 applications, a new error continues to surface even after attempts to resolve the previous one. Following the creation of a custom component mentioned in this error (How to load other page inside of an ionic segment?), a new ...

Developing custom buttons in TinyMCE using Angular 9 for enhanced functionality with dynamic data

I could use some advice. I am in the process of incorporating tinymce-angular into my Angular 9 project. So far, I have successfully loaded and displayed the editor, as well as saved data. Now, I would like to add buttons/dropdown-lists that will offer dy ...

Angular 2 - illuminate modified data cell in table

Is there a way to highlight a table cell that has changed its value? <tr *ngFor="#product of products" (click)="onSelect(product)"> <td>{{product.productName}}</td> <td>{{product.price}}</td> </tr> Within the c ...

What causes socket.io to display the message "Bad handshake method"?

Hey there, I'm currently having some issues with a socket connection. I am using socket.io in the nodejs-express backend and ngx-socket-io in the angular frontend. However, during the connection process, I am receiving a 400 status ({"code": ...

Azure deployment for Angular app was unsuccessful due to npm startup failure

I've been trying to deploy my Angular application on Azure. While I can successfully publish it using Azure, I keep encountering an error message when trying to access the published site: AggregateException: One or more errors occurred. (One or more ...

Retrieving class properties in typescript

I am working with a class that has numerous methods, which I refer to as myClass. When calling it, the syntax is as follows: myClass[key]() Is there a way to retrieve the valid values for key? I tried using keyof myClass, but received an error message st ...

typescript import { node } from types

Exploring the possibilities with an electron application developed in typescript. The main focus is on finding the appropriate approach for importing an external module. Here is my typescript configuration: { "compilerOptions": { "target": "es6", ...

What methods are available to extract HTML elements from .ts files?

Exploring Angular development has been quite a challenge for me. I've heard that typescript is an extension of javascript, but when it comes to manipulating HTML elements in the .ts file, I seem to be at a loss. I attempted a simple document.getEleme ...

Turning off the warning in VS 2019: Stop receiving the message prompting you to install the 'Microsoft.TypeScript.MSBuild' NuGet package for enabling TypeScript compilation in your project

Is there a way to prevent the warning message prompting me to install the 'Microsoft.TypeScript.MSBuild' NuGet package for TypeScript compilation in my project? I prefer using custom tools for TS compilation, such as building webpack bundles man ...

Angular 4 Password Regular Expression Not Working

My validation regex checks for at least 8 characters, including one number, one uppercase letter, one lowercase letter, and one special character. Here is the regex I am using: '(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-zd$@$!%*?& ...

Using Angular 2 to implement conditional statements within an anchor tag

Here's a code snippet I'm working with: <a href="{{banner.url}}"></a> Sometimes the url value is null. How can I check for this and conditionally pass it to href? I attempted the following approach: <a href="{{banner.url === nu ...

Extending the Object prototype within an ES6 module can lead to errors such as "Property not found on type 'Object'."

There are two modules in my project - mod1.ts and mod2.ts. //mod1.ts import {Test} from "./mod2"; //LINE X interface Object { GetFooAsString(): string; } Object.prototype.GetFooAsString = function () { return this.GetFoo().toString(); } //mod2. ...

Leveraging TypeScript's "this" keyword within an interface

I am currently working on a personalized interface where I aim to determine the type of an interface value within its implementation rather than in the interface definition, without using generics. It is important to note that these implementations will al ...

I am curious about the types of props for the methods within the 'components' object in react-markdown

Having some trouble using 'react-markdown' in NextJs 13 with typescript. TypeScript is showing errors related to the props of the 'code' method, and after searching online, I found a solution that involves importing 'CodeProps&apos ...

Angular will automatically update the fields of an object if they have been changed

When working with a component that needs to update an object based on field changes, there are different approaches you can take. One common method is to use a getter in the onInit lifecycle hook to fetch data from a database and then update the fields acc ...

Error: Code layer not located while utilizing "sam invoke local" in AWS

Currently, I am engaged in an AWS project where I am developing two lambda functions. Both of these functions rely on a common codebase stored in the node_modules directory, which is placed in a separate layer named AWS::Lambda::LayerVersion, not to be con ...

Navigating in Angular using a "complete URL" (URL with parameters) can be achieved with the following steps:

My search page is equipped with numerous filter options, all of which are included as parameters in the URL for easy sharing. When moving from the search results to a detailed view of an item, I pass the complete search result URL as a parameter (detail/1? ...