# HTTP integration

**URL:** <https://www.thethingsnetwork.org/forum/t/http-integration/5271>\
**Category:** Uncategorized\
**Created:** [February 7, 2017, 10:17am UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271 "2017-02-07T10:17:49Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![rmeldo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/rmeldo/32/34378_2.png) [@rmeldo](https://www.thethingsnetwork.org/forum/u/rmeldo)\
**Post date:** [September 3, 2017, 6:04pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/21 "2017-09-03T18:04:24Z")

</div>

Hi,

i hope I can get some help.

I followed the example above and I managed to get the payload out of JSON and into my database.

Now working on the security and I am stuck on trying to process the headers sent btythe ttn http integration.

I used [https://requestb.in](https://requestb.in) and I see all the headers there, including:

```
Host: requestb.in
Content-Length: 635
User-Agent: http-ttn/2.5.1
Content-Type: application/json
Accept-Encoding: gzip
Cf-Connecting-Ip: 
X-Request-Id: 
Cf-Visitor: {"scheme":"https"}
Via: 1.1 vegur
Cf-Ray: 
Connect-Time: 0
Authorization: dummy_auth
Cf-Ipcountry: IE
Total-Route-Time: 0
Connection: close
add-Key: dummy_key

```

However, when I try to parse the headers in my PHP script using the function `getallheaders()` I only get a subset of the above, specifically:

```
Host: requestb.in
Content-Length: 635
User-Agent: http-ttn/2.5.1
Content-Type: application/json
Accept-Encoding: gzip

```

I don’t have much familiarity with servers and PHP so I don’t know whether the extra headers were stripped out by the server, not picked up by the `getallheaders()` function or something else.

Any advice would be appreciated.

Thanks  
Riccardo

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [September 3, 2017, 8:41pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/22 "2017-09-03T20:41:45Z")

</div>

> [@rmeldo](#):
>
> when I try to parse the headers in my php script using the function getallheaders() I only get a subset of the above, specifically:
> 
> Host: requestb.in  
> …

That value for `Host` cannot be true; it should of course be the domain of your very own server.

Most other headers you see with RequestBin are specific to their setup at Cloudfare. (Think: headers added by their load balancer, SSL off-loading, CDN.)

So, the HTTP headers you’re seeing on your own server are probably fine. [Not quite; see answers below.]

> [@rmeldo](#):
>
> I managed to get the payload out of JSON and into my database

Why do you think you need more headers then?

~~From my post above:~~ From the initial version (meanwhile edited) of my post above:

> [@arjanvanb](#):
>
> Also, to ensure the requests are really coming from TTN: I’d add some authentication to the URL (something like `tttpost.php?myauth=my-super-secret`), or check the key in `downlink_url` against what you know is correct, or use a random URL (like `my-super-secret.php`).

That ~~does not~~ did not refer to additional HTTP headers (which were not yet supported at the time of writing), but to [URL query parameters](http://php.net/manual/en/reserved.variables.get.php) and the content of the JSON payload.

---

<div class="post-metadata">

**Author:** ![rmeldo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/rmeldo/32/34378_2.png) [@rmeldo](https://www.thethingsnetwork.org/forum/u/rmeldo)\
**Post date:** [September 3, 2017, 10:23pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/23 "2017-09-03T22:23:36Z")

</div>

Yes, I copied and pasted the host from the lines above when writing the questions. I get the right one in PHP.

With regards to the security I was trying to do it by the book and follow the ttn implementation for the http integration, using the  
Authorization: dummy\_auth and additional optional key:  
add-Key: dummy\_key.  
However it appears difficult to do, so ?myauth=my-super-secret will have to do. Also I can use the json fields for further authentication.  
Thanks for the advice  
Riccardo

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [September 4, 2017, 5:35am UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/24 "2017-09-04T05:35:10Z")

</div>

If you’re saying you provided values for `Authorization` and `add-Key` and got those in RequestBin but not in your own server, then that seems to be weird indeed.

(Next time please copy the exact headers.)

---

<div class="post-metadata">

**Author:** ![rmeldo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/rmeldo/32/34378_2.png) [@rmeldo](https://www.thethingsnetwork.org/forum/u/rmeldo)\
**Post date:** [September 4, 2017, 8:24am UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/25 "2017-09-04T08:24:07Z")

</div>

Will do.

Maybe someone could post the PHP code they use to extract, parse and use the headers?  
It is good to start with something known to work.

Thanks for the advice  
Riccardo

---

<div class="post-metadata">

**Author:** ![Charles](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/charles/32/45146_2.png) [@Charles](https://www.thethingsnetwork.org/forum/u/Charles)\
**Post date:** [September 4, 2017, 11:08am UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/26 "2017-09-04T11:08:00Z")

</div>

For this (and any IoT action) I would use node red doing something as simple as that  
 ![](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/a/aa0aaea4b153d514c26bacdcb3434a65362df5b1.png)

Check this [link](https://ictoblog.nl/2017/04/15/ttn-mqtt-node-red-mysql-local-backup-of-your-lorawan-data)

Decode function should be something like that

```javascript
var devID;
var dat;
devID = msg.dev_id;

// Decode data received my nodes are loranode01 or loranode02, ..
if ( devID.match(/loranode\d{2}/) ) {
    console.log("Valid devID=%s", devID);
    dat = decode_loranode(msg.payload_raw);
    console.log("bat=%d temp=%d hum=%d", dat.battery, dat.temperature, dat.humidity);

    strSQL = "INSERT INTO weather (NODE, TEMPERATURE, HUMIDITY, BATT, FRAMECOUNT) "
    strSQL += "VALUES ( ";
    strSQL += dat.devID + ", ";
    strSQL += dat.temperature + ", ";
    strSQL += dat.humidity + ", ";
    strSQL += dat.battery + ", ";
    strSQL += dat.fc + ", ";
    strSQL += ") ";
    console.log("MySQL=%s", strSQL);
        msg.payload = strSQL;
    return msg
} else {
    console.log("Invalid devID=%s", devID);
}

function decode_loranode(data) {
  var bytes = new Buffer(data, 'base64');
  var battery = (bytes[2] << 8) | bytes[3] ;
  var fc = (bytes[6] << 8) | bytes[7] ;
  var temperature = ((bytes[10] << 8) | bytes[11]) / 10;
  var humidity = bytes[14] / 2;
  console.log("batt=%d fc=%d temp=%d hum=%d", battery, fc, temperature, humidity);
  return {
    battery: battery,
    framecount: fc,
    humidity: humidity,
    temperature: temperature};
}

```

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [September 4, 2017, 4:55pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/27 "2017-09-04T16:55:02Z")

</div>

> [@Charles](#):
>
> strSQL = "INSERT INTO weather (NODE, TEMPERATURE, HUMIDITY, BATT, FRAMECOUNT) "  
> strSQL += "VALUES ( ";  
> strSQL += dat.devID + ", ";

…ai, what if I figure out the URL you’re using, and then send a value for `fc` that is something like `1); drop table weather; --`…?

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [September 4, 2017, 5:29pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/28 "2017-09-04T17:29:02Z")

</div>

> [@rmeldo](#):
>
> using the  
> Authorization: dummy\_auth and additional optional key:  
> add-Key: dummy\_key

The `Authorization` header not being visible [might be a known issue](http://php.net/manual/en/features.http-auth.php) on some servers, if the server only supports Basic/Digest authorization, or is strict about the `Authorization: <type> <credentials>` header format:

> In order to get HTTP Authentication to work using IIS server with the CGI version of PHP you must edit your IIS configuration “_Directory Security_”. Click on “_Edit_” and only check “_Anonymous Access_”, all other fields should be left unchecked.

…and:

> Workaround for missing `Authorization` header under CGI/FastCGI Apache:

> ```
> SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
> 
> ```

> Now PHP should automatically declare `$_SERVER[PHP_AUTH_*]` variables if the client sends the `Authorization` header.

(It’s an old reference and I’ve not read any of the other comments.)

Also, [on Stack Overflow Mun Mun Das writes](https://stackoverflow.com/questions/11990388/request-headers-bag-is-missing-authorization-header-in-symfony-2/11992664#11992664):

> Authorization header […] is discarded by apache if not in valid format. Try using another name.

…to which Mika Tuupola responded:

> AFAIK Apache will accept any format Authorization header. It is PHP which discards anything else than valid Basic or Digest header.

Either way, surely “`dummy_auth`” is not a valid value as it does not follow the syntax `Authorization: <type> <credentials>`, [such as `Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l `](https://en.wikipedia.org/wiki/Basic_access_authentication#Client_side).

All this would not explain why `add-Key` would be missing, if you’ve indeed configured that too.

(@rmeldo, meanwhile I understand that nowadays the HTTP Integration supports setting an `Authorization` header or custom header, which [it did not support](https://www.thethingsnetwork.org/forum/uploads/default/original/2X/d/dc9e180ff20d5f26a71681315715f07255357539.jpg) in its initial release. I’ve edited my earlier answers for future readers.)

---

<div class="post-metadata">

**Author:** ![Charles](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/charles/32/45146_2.png) [@Charles](https://www.thethingsnetwork.org/forum/u/Charles)\
**Post date:** [September 4, 2017, 6:09pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/29 "2017-09-04T18:09:48Z")

</div>

@arjanvanb  
correct, but it was just a sample to help I do not have any table, and my sql instance is firewalled only with my admin IP 🙂  
but anyway no sql admin would allow internet user to delete data or tablel not ? 😉

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [September 4, 2017, 6:34pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/30 "2017-09-04T18:34:45Z")

</div>

> [@Charles](#):
>
> my sql instance is firewalled only with my admin IP

Though your link refers to using MQTT to subscribe to a TTN server _from_ Node-RED, in the context of the HTTP Integration TTN (or anyone) would be able to post data _to_ Node-RED. With bad data, Node-RED would then craft a SQL statement one might not expect, no matter if the database can be accessed directly from the Internet or not.

> [@Charles](#):
>
> no sql admin would allow internet user to delete data or tablel not ?

Of course, SQL injection is also about exposing data…

There’s never an excuse for a dangerous example! 😉

---

<div class="post-metadata">

**Author:** ![rmeldo](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/rmeldo/32/34378_2.png) [@rmeldo](https://www.thethingsnetwork.org/forum/u/rmeldo)\
**Post date:** [September 4, 2017, 10:08pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/31 "2017-09-04T22:08:28Z")

</div>

Thanks for the tips.  
I will work my way through and report back.  
Riccardo

---

<div class="post-metadata">

**Author:** ![Charles](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/charles/32/45146_2.png) [@Charles](https://www.thethingsnetwork.org/forum/u/Charles)\
**Post date:** [September 6, 2017, 10:22pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/32 "2017-09-06T22:22:17Z")

</div>

@arjanvanb  
Sorry my fault, the Node Red TTN in my stream was over MQTT not HTTP, since the goal was to insert into mySQL data from TTN. Even if original question was with http integration worth mentioning other simple solution can work.

By the way, I do not consider my example as bad, since data (payload) is parsed by a function in between MQTT and mySQL Insert. Each payload byte is decoded to be transformed by number so whatever you push in to the function, I think you would never be able to do any SQL command injection with dangerous commands in this example 😉

Afterward of course the transfer between the output of Node Red to MySQL is relevant to network security and all rules applies (authentication, encryption, firewalling, …)

---

<div class="post-metadata">

**Author:** ![HrMansen](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/hrmansen/32/43880_2.png) [@HrMansen](https://www.thethingsnetwork.org/forum/u/HrMansen)\
**Post date:** [March 14, 2018, 9:38pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/33 "2018-03-14T21:38:38Z")

</div>

I found your code, while working on a PHP script, that react on a uplink message (from HTTP Integration) and making a downlink message to the device.  
I’ve tried to use your code, to make my webserver, though Curl, make a Downlink-message.  
But i do not get any result and no error.  
Could i ask you (or maybe someone else 🙂 ) to make an example, how to do this, or maybe point out my errors in this code:

```php
<?php
//API Url (Got it from Uplink http-Integration, removed the key for this public forum)
$url = "https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/hrmansen1/http?key=ttn-account-v2.[REMOVED]";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
$jsonDataEncoded = "{\"dev_id\":\"hrmansenuno\",\"port\":1,\"confirmed\":false,\"payload_raw\":\"00\"}";
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
 
//Execute the request
$result = curl_exec($ch);
echo "Result:<br/>";
echo $result;
echo "<br>";
echo "Errorcode: ";
echo curl_error($ch)
?>

```

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [March 16, 2018, 10:31am UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/34 "2018-03-16T10:31:33Z")

</div>

> [@HrMansen](#):
>
> ```
> \"payload_raw\":\"00\"
> 
> ```

That doesn’t look like being Base64 encoded?

---

<div class="post-metadata">

**Author:** ![HrMansen](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/hrmansen/32/43880_2.png) [@HrMansen](https://www.thethingsnetwork.org/forum/u/HrMansen)\
**Post date:** [March 16, 2018, 11:51am UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/35 "2018-03-16T11:51:07Z")

</div>

It isn’t 🙂

- i’ll try to change that to “MDA=” when i get home.

Thanks…

---

<div class="post-metadata">

**Author:** ![HrMansen](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/hrmansen/32/43880_2.png) [@HrMansen](https://www.thethingsnetwork.org/forum/u/HrMansen)\
**Post date:** [March 16, 2018, 7:00pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/36 "2018-03-16T19:00:29Z")

</div>

Unfortunately it did’nt change anything. I changed the line for the Json message to:  
`$jsonDataEncoded = "{\"dev_id\":\"hrmansenuno\",\"port\":\"1\",\"confirmed\":\"false\",\"payload_raw\":\"" . base64_encode("00") . "\"}";`

But still get no result at the device, and no line in the Data log in the console.  
Any ideas or php examples are welcome 😉

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [March 16, 2018, 7:34pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/37 "2018-03-16T19:34:49Z")

</div>

> [@HrMansen](#):
>
> ```php
> \"port\":\"1\",\"confirmed\":\"false\"
> 
> ```

And now these are no longer a number and boolean, like they were earlier, and like [they should be](https://www.thethingsnetwork.org/docs/applications/http/#with-raw-payload). Doesn’t TTN return some error that shows in your `echo` statements? If not, try with `curl -v` on the command line first? See [Downlink from HTTP integration does not show in TTN Console and does not reach my device](https://www.thethingsnetwork.org/forum/t/downlink-from-http-integration-does-not-show-in-ttn-console-and-does-not-reach-my-device/10330) and [Using curl or Postman for the HTTP Integration on Windows](https://www.thethingsnetwork.org/forum/t/using-curl-or-postman-for-the-http-integration-on-windows/12467).

Also, if this would work, you would send _the text_ `00`, which might not be what you want to send?

---

<div class="post-metadata">

**Author:** ![HrMansen](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/hrmansen/32/43880_2.png) [@HrMansen](https://www.thethingsnetwork.org/forum/u/HrMansen)\
**Post date:** [March 18, 2018, 1:18pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/38 "2018-03-18T13:18:42Z")

</div>

Hi Again.  
I have experimented a little more since last time. I have found that my post date could be reduced to just dev\_id and payload\_raw.  
I also found some example code from another project, and now i got my downlink message to be shown in the Data section of my device. But somehow my base64 encoding, ends with a extra 30 added to every digit 🙂  
Just now while i am typing this, realizing that its the 30hex from the ascii character “0” 🙂

Thanks for the help!

Here’s my corrected code if anybody else is interested:

```php
$data = array("dev_id" => "hrmansenuno","payload_raw" => base64_encode(chr(0) . chr(1)));
$url ="https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/hrmansen1/http?key=ttn-account-v2.rNA<redacted>YWiQ";

$post = json_encode($data);
echo $post;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
echo " " . curl_exec($ch);
echo "<br>Errorcode: " . curl_error($ch);
curl_close($ch);

```

---

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [March 18, 2018, 1:44pm UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/39 "2018-03-18T13:44:56Z")

</div>

> [@HrMansen](#):
>
> somehow my base64 encoding, ends with a extra 30 added to every digit

Yes:

> [@arjanvanb](#):
>
> Also, if this would work, you would send _the text_ `00`, which might not be what you want to send?

> [@HrMansen](#):
>
> `base64_encode(chr(0) . chr(1))`

That probably indeed does the job, assuming you might also want to send much larger values, up tot 255 for each value. If you want to set “on” or “off”, then you can combine 8 states in a single byte. See [https://www.thethingsnetwork.org/docs/devices/bytes.html](https://www.thethingsnetwork.org/docs/devices/bytes.html)

---

<div class="post-metadata">

**Author:** ![loes7815](https://www.thethingsnetwork.org/forum/letter_avatar_proxy/v4/letter/l/f08c70/32.png) [@loes7815](https://www.thethingsnetwork.org/forum/u/loes7815)\
**Post date:** [May 3, 2018, 7:36am UTC](https://www.thethingsnetwork.org/forum/t/http-integration/5271/40 "2018-05-03T07:36:57Z")

</div>

Hi,  
I’m not sure if I need to open a new topic, but all the above is also applicable for my question.  
I’m trying to do the same, but I get stuck in the part before this topic starts;  
I have webhook.site working, but now I can’t figure out what to fill in in the http integration window to make  
$content = file\_get\_contents(“php://input”);  
to work in my php-script.

I’m now locally working with xampp, so localhost. But i guess I need to place my php script somewhere on a server?  
I have a Azure and visualstudio environment available (also where my database and API is). But dont know what is needed.  
Thanks.

[Previous page](https://www.thethingsnetwork.org/forum/t/http-integration/5271.md?page=1)

[Next page](https://www.thethingsnetwork.org/forum/t/http-integration/5271.md?page=3)
