Forum Discussion

VarunGrazitti's avatar
9 years ago

How to evaluate values from request parameters when we hit with formData method

We are facing some issues in interaction of component with endpoint. When we make a call from component to endpoint with {key:value} syntax then we can successfully get these request variables at endpoint, but when we try  in Form Data, then we do not get that data on endpoint.

 

Here is a similar unanswered query

 

Component Code:

var URL = "${endpoint_url}"; 
var fd = new FormData(); 
fd.append('title', "abc"); 
fd.append('description', "jkl"); 
var req = { 
method: 'POST', 
url: URL, 
headers: { 
	'Content-Type': undefined 
}, 
	data: fd 
}
$http(req). 
then(function(response) { 
	console.log(response); 
}, 

Endpoint Code:

<#assign title = http.request.parameters.name.get("title","") /> // if we evaluate this value it is blank 
<#assign description = http.request.parameters.name.get("description","") /> // if we evaluate this value it is blank 

Also, with the case of attachment upload, if we append a file variable, then how can we get this on endpoint?

 

Thanks in advance

  • http.request.parameters.name.get is for normal http post and get, not for multipart. Not sure about the freemarker syntax for endpoint. You may need professional service to do the file upload at Java Level. Please correct me if I am wrong.

    VarunGrazitti Can you give a user case? Maybe we can tackle it in another way.

    • manpreetsingh's avatar
      manpreetsingh
      Contributor

      Hi peterlu,

       

      Use case is:
      We want to upload an attachment from component to our server that is providing us a third party api (multipart support). In that case we need to develop a component and EndPoint in lithium from where we can push our attachment.

       

      Explanation and requirement: We want to hit our third party api via lithium EndPoint and not directly from lithium component, so our problem is how do we upload an attachment from lithium component via Endpoint to that server?

       

      Other options: multipart approach requires form data, but if we are not using multipart then we can convert the attachment into base 64 string but in that case it exceeds its limit if the attachment is large in size.

       

      Any help will be appreciated !

      • peterlu's avatar
        peterlu
        Champion

        manpreetsingh if the attachment is small, you can base64 encode it, and send to endpoint. (Please check bowen's answer for angular usage). Then you can use http client in endpoint to post to your thirt party server.

         

        But if the attachment is big, then it may not work properly. You may need to limit the file size by frontend checking. And also you need to check the base64 string size at the endpoint.

         

        Endpoint may not work for your case if you cannot predict the file size. You may need professional service to help you write the java object at a lower level.

  • 					data:$httpParamSerializer({
    						key:val
    					}),
    					headers:{
    					 "Accept":"*/*",
    					 "X-Requested-With":"XMLHttpRequest",
    					 "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
    					}

    This will solve your problem in Angular JS env.