Forum Discussion

perk's avatar
perk
Adept
9 years ago

HTTP client URL encoding

Hi,

 

I am wondering if anyhow knows how to get the HTTP client to work with special characters.

 

I have a URL string that contains "+" and ":". If I use the FreeMarker ?url method for the string, I get something like:

 

https%3A%2F%2Fabc.com%2Fapi%2Fv2%2Fsearch.json%3Fpage%3D2%26query%3Dtabc%253Acba%2Babc%253Acba

 

which fails in Lithium with this error message:

 

 Method public final lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel lithium.coreapi.webui.template.models.HttpClientTemplateModel.request(java.lang.String) throws freemarker.template.TemplateModelException threw an exception when invoked on lithium.coreapi.webui.template.models.HttpClientTemplateModel object "lithium.coreapi.webui.template.models.HttpClientTemplateModel@e453be1" with arguments of types [java.lang.String,]. See cause exception. The failing instruction (FTL stack trace):

 

If I manually replaced the "+" with " " and leave ":" as decoded, the call fails intermittently with the same error.

 

 

  • I think OlivierS is right with the 1st hint, when looking at the docs here http://community.lithium.com/t5/Developers-Knowledge-Base/http-client-FreeMarker-Context-Object/ta-p/80614 the URLs in the example are also not encoded and as far as I remember without looking at my code, i never encoded the request URL either...

     

    Also the second point is important, where does it fail, when you just have the http.client making the request or when code is present that is supposed to print the response (for example)? In that case you'd have to use the http.client.response context object!

    • perk's avatar
      perk
      Adept

      I have a line in freemarker that does:

       

      <#assign response_nextTicket = http.client.request(url).header("Authorization", token).header("Content-type", "application/json").json().get() />

      This is the line that is failing. If the url contains "+" and ":" in their decoded form,

       

      https://xxx?page=2&query=type:ticket+organization:123 

       

      This is the error I get: 

       

      FreeMarker template error

       Method "public final lithium.coreapi.webui.template.models.HttpClientResponseTemplateModel lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel.get() throws freemarker.template.TemplateModelException" threw an exception when invoked on lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel object "lithium.coreapi.webui.template.models.HttpClientRequestTemplateModel@15cd265b". See cause exception. The failing instruction (FTL stack trace): ---------- ==> #assign response_nextTicket = http.cl...

      at the bottom, I noticed a null pointer error:

      Caused by: freemarker.template.TemplateModelException: No error description was specified for this error; low-level message: java.lang.NullPointerException: null The failing instruction (print stack trace for 15 more): ==> #assign response_nextTicket = http.cl...

       Any further thoughts on this?

      • phani's avatar
        phani
        Advisor

        Hi perk,

         

        Have you tried this http.client call with any other URL which don't have "+"? is it working?

        If not i am sure Lithium have to perform some configuration at backend to enable http.client access for your community. Contact support on this.,

        If it is working then could you please try again with encoded query data(not full URL).

        i.e.

        <#assign query1 = "type:ticket+organization:123"?url/>

          <#assign URL =" https://xxx?page=2&query=${query1}"/>

         

        Hope this helps!

         

        Regards,

        Phani

  • OlivierS's avatar
    OlivierS
    Lithium Alumni (Retired)

    perk

     

    1- what happens if you don't encode the url at all?

    2- you're saying that when encoded it fails in Lithium. Could you please provide more information? (e.g. where is it failing?)