Jump to content

WHMCS Profile custom field not taking data input via API


Samar

Recommended Posts

Hi there,

 

I was trying to create an api that updates the customfields of WHMCS. Our api backend is in golang using the echo framework. This is how I am trying to make the request

func updateShowOnboard(c echo.Context) error {

                var customFieldsArray []*CustomFields

                customField := &CustomFields{}

                customField.id = 3

                customField.value = "panelPassword"

                customFieldsArray = append(customFieldsArray, customField)

                customField1 := &CustomFields{}

                customField1.id = 7

                customField1.value = "howdidyoufindus"

                customFieldsArray = append(customFieldsArray, customField1)

                customField2 := &CustomFields{}

                customField2.id = 8

                customField2.value = "gstinhere"

                customFieldsArray = append(customFieldsArray, customField2)

                customField3 := &CustomFields{}

                customField3.id = 11

                customField3.value = "No"

                customFieldsArray = append(customFieldsArray, customField3)

                finalcustomfieldArray, err8 := json.Marshal(customFieldsArray)

                if err8 != nil {

                                return c.JSON(http.StatusBadRequest, errorMsg(err8))

                }



                url := os.Getenv("WHMCS_ENDPOINT")

                method := "POST"

                payload := &bytes.Buffer{}

                writer := multipart.NewWriter(payload)

                _ = writer.WriteField("username", os.Getenv("WHMCS_IDENTIFIER"))

                _ = writer.WriteField("password", os.Getenv("WHMCS_SECRET"))

                _ = writer.WriteField("clientid", "490")

                _ = writer.WriteField("customfields", string(finalcustomfieldArray))

                _ = writer.WriteField("responsetype", "json")

                _ = writer.WriteField("action", "UpdateClient")

                err1 := writer.Close()

                if err1 != nil {

                                return c.JSON(http.StatusBadRequest, errorMsg(err1))

                }



                client := &http.Client{}

                req, err2 := http.NewRequest(method, url, payload)



                if err2 != nil {

                                return c.JSON(http.StatusBadRequest, errorMsg(err2))

                }



                req.Header.Set("Content-Type", writer.FormDataContentType())

                res, err3 := client.Do(req)

                if err3 != nil {

                                return c.JSON(http.StatusBadRequest, errorMsg(err3))

                }



                defer res.Body.Close()

                body, err4 := io.ReadAll(res.Body)

                if err4 != nil {

                                return c.JSON(http.StatusBadRequest, errorMsg(err4))

                }



                m := map[string]string{}

                json.Unmarshal(body, &m)

                if m["result"] != "success" {

                                return c.JSON(http.StatusBadRequest, errorJSON{"Some error occured"})

                }

                return c.JSON(http.StatusOK, m)

}

 

as you can see that I am creating an array and appending customfields object with id and value to it, later I am converting it into json and converting the json to a string value and passing it to the customfields field in the payload. I get success message from the api but the values don’t change.

 

If you could point me to right direction that would be immensely helpful thanks.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated