Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb HTTP PUT Request

    I am trying to create a HTTP put request to turn on and off my Philips hue bulbs. I can't seem to figure it out. I have to make it embed able into an HTML webpage. Can anyone here guide me in the right direction? I am new to programming and I have spent the last 2-3 hours trying to figure it out with no luck. It's needs to have a url and a body too. Thanks!

  2. #2
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,430
    Thanks
    30
    Thanked 878 Times in 876 Posts
    See post two here: http://www.codingforums.com/html-css...ml#post1410056 And then please post some code (what you have tried) or a link to your website.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  3. #3
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is the code I used:

    <script language="javascript">
    var url = "http://192.168.1.12/api/newdeveloper/lights/1/state";
    var representationOfDesiredState = {"on":false}

    var client = new XMLHttpRequest();

    client.open("PUT", url, false);

    client.send(representationOfDesiredState);

    if (client.status == 200)
    alert("The request succeeded!\n\nThe response representation was:\n\n" + client.responseText)
    else
    alert("The request did not succeed!\n\nThe response status was: " + client.status + " " + client.statusText + ".");

    </script>

    It responds with "The request succeeded! The response representation was: [{"error":{"type":2,"address":"/lights/1/state","description":"body contains invalid json"}}]

  4. #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,872
    Thanks
    12
    Thanked 401 Times in 398 Posts
    I'm no JS programmer, but it seems odd that you never use the variable "url", and also this line has a missing quote:

    client.open("PUTl, false);

  5. #5
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I fixed the quote but I'm still getting the same response.

  6. #6
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,430
    Thanks
    30
    Thanked 878 Times in 876 Posts
    See what this does for you:
    Code:
    <script type="text/javascript">
    var url = "http://192.168.1.12/api/newdeveloper/lights/1/state?on=false";
    var client = new XMLHttpRequest();
    
    client.open("GET", url, true);
    client.send();
    
    if (client.status == 200)
    	alert("The request succeeded!\n\nThe response representation was:\n\n" + client.responseText)
    else
    	alert("The request did not succeed!\n\nThe response status was: " + client.status + " " + client.statusText + ".");
    </script>
    Last edited by sunfighter; 09-04-2014 at 10:56 PM.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  7. #7
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I got "The request did not succeed! The Response status was: 0 ." for the code that you gave me. I also tried changing the "GET" to "PUT" but I got the exact same message.

  8. #8
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,430
    Thanks
    30
    Thanked 878 Times in 876 Posts
    So the problem must be with http://192.168.1.12/api/newdeveloper/lights/1/state

    Please post
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  9. #9
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,872
    Thanks
    12
    Thanked 401 Times in 398 Posts
    I suspect this is a service with a REST interface for which you don't have source code. Are you able to put:

    http://192.168.1.12/api/newdeveloper...state?on=false

    in your browser and get the correct response?


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •