Forum Discussion

jasonnitta's avatar
9 years ago

Custom Endpoint: Update user settings/profile data

Hello! I'm new to the Freemarker language and somewhat new to Lithium. I'm trying to create a custom endpoint that takes parameters from a call from a Java service and uses them to update a user's profile. So far I have tried to update the email address with no luck. I'm getting a 500 error when calling this endpoint. Here's what I have so far:

 

<#include "common-vars.ftl"/>
<#attempt>
    <#assign username = http.request.parameters.name.get("username")/>
    <#recover><#assign username = ""/>
</#attempt>
<#attempt>
    <#assign firstName = http.request.parameters.name.get("firstName")/>
    <#recover><#assign firstName = ""/>
</#attempt>
<#attempt>
    <#assign lastName = http.request.parameters.name.get("lastName")/>
    <#recover><#assign lastName = ""/>
</#attempt>
<#attempt>
    <#assign email = http.request.parameters.name.get("email")/>
    <#recover><#assign email = ""/>
</#attempt>
<#attempt>
    <#assign aboutMe = http.request.parameters.name.get("aboutMe")/>
    <#recover><#assign aboutMe = ""/>
</#attempt>
<#attempt>
    <#assign avatar = http.request.parameters.name.get("avatar")/>
    <#recover><#assign avatar = ""/>
</#attempt>
<#attempt>
    <#assign userIdQry = "SELECT id from users where login = '${username}'"/>
    <#assign userId = executeLiQLQuery(userIdQry)/>
    <#recover><#assign userId = ""/>
</#attempt>

<#function updateEmail username email>
    <#return (rest("/restapi/vc/users/login/" + username + "/email/set/" + email))/>
</#function>

<#attempt>
    ${restadmin("/restapi/vc/users/login/" + ${username} + "/email/set/" + ${email}}
    <#recover>There was an error updating email.
</#attempt>

As you can see from the code, there are a few other fields that we would like to update via this endpoint. Thank you for your help!

 

EDIT: If there is a way to use API v2 to update multiple fields with one call, that would be extremely helpful!

  • jasonnitta - The issue is in the updateEmail function. You don't need to add  /restapi/vc to the call, just make the rest call as 

     

     <#return (rest("/users/login/" + username + "/email/set/" + email))/>

     

    I hope this helps.

    • jasonnitta's avatar
      jasonnitta
      Guide

      Thank you VarunGrazitti for your suggestion. Fortunately I'm not getting the 500 error anymore. Unfortunately, the error has changed to:

       

      <error code="501">
      <message>Unknown path element at node 'user.email.set'.</message>
      </error>

      I got that after changing the function to use the "restadmind" context object. My modified code looks like this:

       

      <#function updateEmail username email>
          <#return (restadmind("/users/login/" + username + "/email/set/" + email))/>
      </#function>
      
      <#attempt>
          ${updateEmail(username, email)}
        	Updated email to ${email}
          <#recover>There was an error updating email.
      </#attempt>

      Am I not supposed to put the email to change to after the /set/ ? Thanks for all your help!

      • VarunGrazitti's avatar
        VarunGrazitti
        Boss
        jasonnitta - Why are you using restadmind? It is used to debug and print the output of restadmin calls in XML or JSON format.

        You'd want to use only restadmin. Did you try it?