简体   繁体   中英

HUE API JavaScript HTTP PUT Request returning “net::ERR_EMPTY_RESPONSE”

  1. GET requests work fine.

  2. PUT requests from the API Debug tool work fine.

  3. PUT and POST, no matter the data or API url that is used, return the following error:

    example:

    OPTIONS http://xxx.xxx.xx/api/AuthorizedUsername/APIRequest/lights/light/1/state net::ERR_EMPTY_RESPONSE

It returns a null response.

My relevant Angular JS code:

var bridgeIp = "http://xxx.xxx.x.x";
var username = "authenticatedUsername";
var lightData = {"on": true};

$http.put(bridgeIp+"/api/"+username+"/lights/5/state", lightData)
.then(function success(response) {
          console.log(response.data);
     }, function error(e){
          console.log(e);
     });

I've also tried using JQuery's Ajax function and received the same issue.

I compared the requests using fiddler, but that caused a CORS error. So I then compared the requests with Charles. Here's those results:

From API Debug Tool:

URL http://xxx.xxx.x.x/api/AuthorizedUser/lights/5/state
Status  Complete
Response Code   200 OK
Protocol    HTTP/1.1
SSL -
Method  PUT
Kept Alive  No
Content-Type    application/json
Client Address  /127.0.0.1
Remote Address  xxx.xxx.x.x/xxx.xxx.x.x
Timing  
Request Start Time  3/23/16 18:24:17
Request End Time    3/23/16 18:24:17
Response Start Time 3/23/16 18:24:17
Response End Time   3/23/16 18:24:17
Duration    43 ms
DNS 0 ms
Connect 2 ms
SSL Handshake   -
Request 2 ms
Response    1 ms
Latency 37 ms
Speed   20.46 KB/s
Response Speed  879.88 KB/s
Size    
Request Header  427 bytes
Response Header 421 bytes
Request 12 bytes
Response    41 bytes
Total   901 bytes
Request Compression -
Response Compression    -

From my http request through JavaScript:

URL http://xxx.xxx.x.x/api/AuthorizedUser/lights/5/state
Status  Failed
Failure Remote server closed the connection before sending response header
Response Code   -
Protocol    HTTP/1.1
SSL -
Method  OPTIONS
Kept Alive  No
Content-Type    -
Client Address  /127.0.0.1
Remote Address  xxx.xxx.x.x/xxx.xxx.x.x
Timing  
Request Start Time  3/23/16 18:24:55
Request End Time    3/23/16 18:24:55
Response Start Time -
Response End Time   3/23/16 18:24:55
Duration    14 ms
DNS 0 ms
Connect 2 ms
SSL Handshake   -
Request 2 ms
Response    -
Latency -
Speed   31.74 KB/s
Response Speed  -
Size    
Request Header  455 bytes
Response Header -
Request -
Response    -
Total   455 bytes
Request Compression -
Response Compression    -

Any help is appreciated.

This works for me:-

 var xmlhttp = new XMLHttpRequest(); xmlhttp.open("PUT", 'http://<local address>/api/<username>/groups/1/action'); xmlhttp.send('{"on":true, "hue": 16000, "bri": 254'}'); 

Your JavaScript output shows Method as OPTIONS. If that is the OPTIONS listed at the top of the posting, then that looks wrong, since it says ...APIRequest/ lights/light /1/state, but should probably be ...APIRequest/ lights /1/state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM