Angular 5 HttpParams I do not detect parameters

0

With the following code when sending a POST with HTTPClient of Angular 5

     let params = new HttpParams({     // ==== NO FUNCIONA BIEN  ¿? !!
      fromObject: {
        topic_id: topic_id.toString(),
        answer_id: answer_id.toString(),
        question_id: question_id.toString(),
        ...this.commonParams()
      }
    });

    let params2 = {                      // ==== así SI funciona
      topic_id: topic_id.toString(),
      answer_id: answer_id.toString(),
      question_id: question_id.toString(),
      ...this.commonParams()
    };

    console.log(' ............', params2);
    return this.http.post(this.url + '/respuesta', params2);

If I use "param" I do not see the data (or I do not know how to read them on the REST server) and if I use "param2" (an object directly) there's no problem ...

I can not understand why

thanks

    
asked by Dani Morte 14.02.2018 в 15:35
source

2 answers

0

I recommend you in the following way:

let headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');

let params = new HttpParams();

let object = { topic_id: topic_id.toString(),
               answer_id: answer_id.toString(),
               question_id: question_id.toString()};

params = params.set('fromObject', object);

return this.http.put(this.url+'/respuesta', params, { headers: headers })
    
answered by 22.02.2018 в 21:36
-1

If the second form works for you, you should use it (it's simpler, too), but I think the problem is that you've added your nested parameters:

let params = new HttpParams({     // ==== NO FUNCIONA BIEN  ¿? !!
  fromObject: { // <-- Esto sobra
    topic_id: topic_id.toString(),
    answer_id: answer_id.toString(),
    question_id: question_id.toString(),
    ...this.commonParams()
  }
});

Try with

let params = new HttpParams({
    topic_id: topic_id.toString(),
    answer_id: answer_id.toString(),
    question_id: question_id.toString(),
    ...this.commonParams()
});
    
answered by 14.02.2018 в 16:03