Simple download message with HTML <form method="POST">

I have tried (conform the TTN documentation) the following in a HTML-page:

<form method="POST" action="https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/5935br27environment/environment?key=................">
<input type="text" name="json" value='{"dev_id": "5935br27node000","port": 1,"confirmed": false,"payload_raw": "AQIDBA=="}' size="100">
<input type="submit">

and expected the message was queued on the console, but no…
What do I wrong?

A html web for POST is different from an API call. Use postman, curl or a similar tool instead.

1 Like

Is there no simple method to post only one byte (bit: 0/1) as download message?

I simply thought to send a JSON object, with the message for the end device. Only 'm not sure about the name of the JSON object in the form.

What documentation is that? We might want to fix that.

True. The API expects a POST body (payload) to hold bare JSON, but a <form method='post'> will create a body that defines parameters with values. So that’s a dead end.

(You’re not specifying enctype, which then defaults to application/x-www-form-urlencoded. But the other option, multipart/form-data as used for file uploads, won’t help either. Also, the API probably expects Content-Type: application/json or similar.)

However, using JavaScript won’t get you any further either, as then you’ll run into the console showing something like the following:

Access to XMLHttpRequest at … from origin … has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

This cannot be fixed without an intermediate server either; see How to send downlink to HTTP Integration from jQuery?

1 Like

Hello Arjan, so not as easy as I thought. I am an oldfashion HTML hacker and thought I could make it easy with a HTML form. The handling (CGI/PERL) of the uplink data was no problem. Because I do not use JS in the page, I also get no CORS error. But I am going to look (after a lot of reading) for an intermediate server in PERL to solf the problem. Thanks and regards…

There’s a very small chance that using text/plain would be accepted by the TTN API.

text/plain nor application/json are accepted by the TTN API. Is there some error report on the console/server?

enctype='application/json' is not a valid option for the browser, so should be rejected or be replaced by the default. (I’m quite sure it is valid for TTN; it’s just that <form method='post'> cannot use it.)

If there’s any error then that should be shown in your browser’s JavaScript console or network tab. There’s no historical logs you can access on TTN.

The only error/exeption is:

[Exception… “Favicon at “https://integrations.thethingsnetwork.org/favicon.ico” failed to load: Not Found.” nsresult: “0x80004005 (NS_ERROR_FAILURE)” location: “JS frame :: resource:///modules/FaviconLoader.jsm :: onStopRequest :: line 227” data: no] FaviconLoader.jsm:227:22

Thanks…

Ah, my bad: actually, the chance is zero, as <form enctype="plain/text" method="post"> would still post parameter names (as given in <input name="whatever">) along with their values, not only the value(s).

That’s the result of the browser trying to find an icon after navigating to the action URL while posting the data. You’ll want to see what the response of the POST to that action URL is. Make sure to use something like “Persist logs” or “Preserve logs” in your browser’s network tab, to see the result of that very URL.

There are many such tools. Postman and curl are two of them. A html form is not the right tool.

1 Like

I chose the path of Arjan ( intermediate server ), not Postman or curl but PERL/LWP…

#!/usr/bin/perl -w
use strict;
use warnings;
# Create a user agent object
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1");

# Create a request
my $req = HTTP::Request->new(POST => 'https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/5935br27environment/environment?key=ttn-account-v2.xxxxxxxxxxxxx');
$req->content_type('application/json');
$req->authorization_basic("admin", "secret");
$req->content('{"dev_id": "5935br27node000","port": 1,"confirmed": false,"payload_raw": "AQIDBA=="}');

# Pass request to the user agent and get a response back
my $res = $ua->request($req);

# Check the outcome of the response
if ($res->is_success) {
  print $res->content;
} else {
  print $res->status_line, "n";
}

TTN Console
it works.

Now the finishing touch. thank you all and regards…