0

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

3
  • please show a minimal example to reproduce the problem Commented Mar 25, 2016 at 17:44
  • @JimB, I just updated my question Commented Mar 25, 2016 at 17:51
  • your file doesn't have the keys: application_id and application_key Commented Mar 25, 2016 at 17:55

2 Answers 2

2

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"
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.