Kousha
Kousha

Reputation: 36219

Golang JSON struct to lowercase doesn't work

I have a struct:

type Credentials struct {
    Username    string  `json:"username"`
    Password    string  `json:"password"`
    ApplicationId   string  `json:"application_id"`
    ApplicationKey  string  `json:"application_key"`
}

And I've tagged my fields to lowercase them.

However, whenever I include the application tags, those fields become null, i.e. on my POST request I get

{ application_id: '',
  application_key: '',
  password: 'myPassword',
  username: 'myUsername' 
}

But if I remove either of the tag (so remove ApplicatinonId or ApplicationKey tag), then that field does show up

Here is how I set my struct:

func getCredentials() Credentials {
    raw, err := ioutil.ReadFile(os.Getenv("BASE_PATH") + FILE_Credentials)
    if err != nil {
        log.Warn("Could not read credentials file: %s", err.Error())
        os.Exit(1)
    }

    var credentials Credentials
    json.Unmarshal(raw, &credentials)
    return credentials
}

My credential json file is:

{
  "Username": "myUsername",
  "Password": "myPassowrd",
  "ApplicationId": "someID",
  "ApplicationKey": "someString"
}

Then, I post my data with:

credentials := getCredentials()
url := GetLoginURL()

resp, body, requestErr := gorequest.New().
    Post(url).
    Send(credentials).
    End()

But on the server, I get both application_id and application_key as empty strings. But if I remove the corresponding tag, then that field is posted

Upvotes: 0

Views: 1065

Answers (2)

evanmcdonnal
evanmcdonnal

Reputation: 48114

Based off the example file your struct in Go should look like this;

type Credentials struct {
    Username    string  `json:"Username"`
    Password    string  `json:"Password"`
    ApplicationId   string  `json:"ApplicationId"`
    ApplicationKey  string  `json:"ApplicationKey"`
}

You could also approach this from the other end and modify the entries in your file to look like this;

{
  "Username": "myUsername",
  "Password": "myPassowrd",
  "application_id": "someID",
  "application_key": "someString"
}

However, it's more often the case that you can't change the data you're receiving (like when calling a third party API) so you usually end up changing your source. Since you control the file and the API wants lower case I'd recommend changing the files contents to match what you send the API. The other option which is sometimes necessary is to use another type and provide a conversion helper (assuming you controlled neither the file nor the API, you'd need different types for each). The Go encoding packages are very strict. You may be used to things like json.NET which will assign near matches, that isn't the case here. Anything less than an exact match will yield an error from Unmarshal.

Upvotes: 0

dave
dave

Reputation: 64667

It looks like your credential file is wrong (it needs to use the keys application_id and application_key - Go is smart enough to figure out the capitalization, but not underscores):

{
  "Username": "myUsername",
  "Password": "myPassowrd",
  "application_id": "someID",
  "application_key": "someString"
}

Upvotes: 2

Related Questions