Http Put method , tips please

Very new to ionic/webapps/networking , please forgive my ignorance.
I need to create an App which sends simple GET / PUT requests to a server, I’ve implemented the get requests simply with:

        .map(res =>     res.text())
             .subscribe(data => { this.my_data = data;   });

However I’ve failed miserably when trying to send a simple PUT request with a raw body. How may i accomplish this ?

Try doing this

let body = new URLSearchParams();
for(let key in post) {
	body.set(key, post[key]);

And use body.toString() for the body

this.http.put(url, body.toString(), options).subscribe();

Sorry , let me post the code I’m using to make it a little bit more clear what I’m trying to do.
I’ve been trying to use this :

  let body = 1; 
  let headers = new Headers({ 'Content-Type': 'application/json' });
  let options = new RequestOptions({ headers: headers });`

  this.http.put('', body, options)
           response => {
          error => {

But I’m getting the console log:

“IsTrusted”: true"

And in my server logs there are no attempts of PUT requests…

1 Like

Are you handling the OPTIONS request? I believe PUT requests have a preflight, check the network tab in your developer tools, if you use Chrome.

Ok thank you, I fixed the code a bit and now at least the server recieves the requests etc. Tought I still get an error during the parsing due to the raw data… Forgive my ignorance , I’ve always used Lua and the little put requests I’ve done were like:

                http_params.header = '["Content-Type": "Application/json"]'
				http_params.body = '["1"]'
			    network.request( '', "PUT", networkListener, http_params )

In the first example you provided, you’re also setting the content type to be json, but you’re not providing json data. Look at the 2 bodies you provided - they’re not the same :wink:

Thanks for your replies, I’ve tried ‘1’ , [‘1’] , “[‘1’]”… In reality all combinations. I’ve tried to pass it as a string like you suggested too , but I get the same results:
Error parsing body param '1'
Error parsing body param "'1'"

It’s probably something really dumb I just can’t see which is frustrating

      ( chrome extension , my server parses its body with no problems )

This is the simple request I’m trying to do in my ionic app.

I think it might need to be an array with keys instead of a normal array, and then just provide the raw thing, don’t stringify it.

let body = { pin: 1 };
this...put(url, body, ...)

Thank you so much for all the time spent on trying to help me… It works now:
It was a problem in my local network… The correct answer was the first one , simply:

let body =  '["1"]';
let headers = new Headers();
headers.append("Accept" , 'application/json');
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({ headers: headers });

..put(url, body, options)

Sorry for waisting time for an internal network error… Thank you again