0

With Node-Red, I'm trying to set the right parameters for http request, but I'm not able to do it.

the block used

Function code

Http request setting

The query with curl is this:

curl -H "Content-Type: application/json" -X GET -d '{"auth_key":"xf79rra", "payload":null}' https://retepaiot.it/sensordata/0018b1213

and it works.

I think I've made somethings wrong passing auth_key and payload (they are not URL parameters). I use the function to prepare the message:

msg.headers = {    
    "Content-Type": "application/json"
    };


msg.payload = {auth_key:"xf7xxxx9rra", 
    payload:null};
return msg;

and http request for URL:

https://retepaiot.it/sensordata/001xxxx1213

the returned message is:

{ "message": "Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)"}
Fabrizio R.
  • 117
  • 1
  • 14

2 Answers2

0

Look at the "Return type" at the bottom of the node config, you have explicitly asked for a JSON object

hardillb
  • 41,053
  • 9
  • 49
  • 77
  • The reply to the request is json. Anyway, using other options the result does not change. – Fabrizio R. Feb 23 '20 at 11:01
  • That's not what you said in the comments on the question. I explicitly asked what the return from the working `curl` command was. – hardillb Feb 23 '20 at 11:01
  • Sorry, I misunderstood your question. Curl returns the requested data. This is one example: { "data": [ { "id": 310310, "time": "2020-02-23T12:27:07", "device_eui": "0018b22000001213", "rssi": -115, "station": "00800000a00047da", "data": "428001107fe200000000", "frequenza": "868500000", "modulazione": "868500000", "lat": 44.812036, "lon": 10.313763 } ], "message": "OK" } – Fabrizio R. Feb 23 '20 at 11:29
  • Edit the question to include that output. I also suggest you use something like https://hookbin.com/ to compare what is actually sent from both `curl` and `node-red` – hardillb Feb 24 '20 at 12:54
  • My opinion is that using the node-red Http request, the payload is not added if the GET command is used. Is removed. Get does not expect data in the body. Here are some related consideration.https://stackoverflow.com/questions/978061/http-get-with-request-body Curl doesn't do that. The -d flag force the data in the body. The problem is that the called server is out of my domain, I cannot change it, and it requires the body for authorization. Conclusion: I don't know how to solve my problem. – Fabrizio R. Feb 24 '20 at 21:40
  • OK, this looks like a bug. Please raise one [here](https://github.com/node-red/node-red/issues). Make sure you fill in the template properly and you can include a link to this question. I have a quick and dirty fix but it will need to be reviewed by the project – hardillb Feb 24 '20 at 22:14
0

The method GET, called from http request, doesn't manage the payload (body on http). Simply it ignores it. This conforms to the protocol, so it isn't a bug (see the comments).

The REST server, which I'm calling, seems not to be compliant with the GET method.

For now, I've modified the source code of node www request. Next, I'll develop my own node.

It might be useful the modification of http request code, to support even servers not fully compliant.

Fabrizio R.
  • 117
  • 1
  • 14
  • NO! do not build your own node, open the issue as I requested and we can get the core node fixed. Adding yet another node to the list of available nodes is WRONG thing to do. – hardillb Feb 25 '20 at 12:55
  • Ok, I'll open a bug, even if I think it's not really a bug. – Fabrizio R. Feb 25 '20 at 14:42