Hello,
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:
this.http.get('http://192.168.1.175:8080/66dbba4f20144cf09fca08f39fe23ecc/get/V6')
.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();
2 Likes
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('http://192.168.1.180:8080/token/updatepin/V50', body, options)
.subscribe(
response => {
console.log(response.text());
},
error => {
alert(error.text());
console.log(error.text());
});
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( 'http://192.168.1.180:8080/token/updatepin/V50', "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 
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'
or
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