Home > Software design >  I don't want to remove trailing spaces while passing an array as request param in API endpoint.
I don't want to remove trailing spaces while passing an array as request param in API endpoint.

Time:08-03

I am creating an array of strings in angular and am sending that array to an API endpoint as a request parameter. Below is the code for that.

However, when the list is passed in java API endpoint the trailing spaces are removed and I do NOT want to remove that. for example, if the list is ['hello ', 'world '] then I want to pass it as is to API. Instead of that in this below code, it is getting passed as ['hello','world']

Can anyone help me with that?

Angular :

    const lst: any=[];
      for(let i=0;i<data.length;i  ){
        lst.push(data[i][0]);
      }
 xlst=await this.getdatafromAPI(lst);

getdatafromAPI(value:[])
  {
    const header = this.servicename.getHeader();
    this.servicename.addForwardUrlInHeader(header, this.URL  '?param='   value );
    return fetch(
      this.URL,
      header
    ).then(response =>
    {
      if (response.status === 200)
      {
        return response.json();
      } else
      {
        console.log('Unable To Get lst');
      }
    });
  }

API endpoint

    @GetMapping(value="/api/xyz")
    public ResponseEntity<List<abc>> getDatafromDb(@RequestParam List<String> lst){

        List<abc> newarrlst=new ArrayList<>();
        try
        {
            for(int i=0;i<lst.size();i  ){
                abc obj=new abc();
                String propvalue=lst.get(i);
                obj.setproperty1(propvalue);
                obj.setproperty1(map.get(propvalue)); // I already have a map from where I am getting values
                newarrlst.add(obj);
            }

CodePudding user response:

You need to encode your value content.

This can be accomplished with encodeURIComponent:

This will encode spaces, and other special characters, with an escape sequence.

  • Related