Go Down

Topic: Continue errors on http get request (Read 72 times) previous topic - next topic

TheAssalt00

Hi guys, i'm a new user of arduino so i'm not very skilled to use it.
My hardware is:
-Arduino Uno
-Wireless Sd Shield
-Wirelezz module RN-XV 171

I have installed the WiFly library and i have created a mock server with postman with a easly get request that return "ok" massage.

I use a examples skecth from the WiFly library.
The code is:
Code: [Select]
#include <WiFlyHQ.h>
#include <SoftwareSerial.h>
SoftwareSerial wifiSerial(8,9);

//#include <AltSoftSerial.h>
//AltSoftSerial wifiSerial(8,9);

WiFly wifly;

/* Change these to match your WiFi network */
const char mySSID[] = "******";
const char myPassword[] = "******";


const char site[] = "http://*******.mock.pstmn.io";

void terminal();

void setup()
{
    char buf[32];

    Serial.begin(9600);
    Serial.println("Starting");
    Serial.print("Free memory: ");
    Serial.println(wifly.getFreeMemory(),DEC);

    wifiSerial.begin(9600);
    if (!wifly.begin(&wifiSerial, &Serial)) {
        Serial.println("Failed to start wifly");
terminal();
    }

    /* Join wifi network if not already associated */
    if (!wifly.isAssociated()) {
/* Setup the WiFly to connect to a wifi network */
Serial.println("Joining network");
wifly.setSSID(mySSID);
wifly.setPassphrase(myPassword);
wifly.enableDHCP();

if (wifly.join()) {
    Serial.println("Joined wifi network");
} else {
    Serial.println("Failed to join wifi network");
    terminal();
}
    } else {
        Serial.println("Already joined network");
    }

    //terminal();

    Serial.print("MAC: ");
    Serial.println(wifly.getMAC(buf, sizeof(buf)));
    Serial.print("IP: ");
    Serial.println(wifly.getIP(buf, sizeof(buf)));
    Serial.print("Netmask: ");
    Serial.println(wifly.getNetmask(buf, sizeof(buf)));
    Serial.print("Gateway: ");
    Serial.println(wifly.getGateway(buf, sizeof(buf)));

    wifly.setDeviceID("Wifly-WebClient");
    Serial.print("DeviceID: ");
    Serial.println(wifly.getDeviceID(buf, sizeof(buf)));

    if (wifly.isConnected()) {
        Serial.println("Old connection active. Closing");
wifly.close();
    }

    if (wifly.open(site, 80)) {
        Serial.print("Connected to ");
Serial.println(site);


/* Send the request */
wifly.println("GET /test HTTP/1.0 ");
wifly.println();
    } else {
        Serial.println("Failed to connect");
    }
}

void loop()
{
    if (wifly.available() > 0) {
char ch = wifly.read();
Serial.write(ch);
if (ch == '\n') {
    /* add a carriage return */
    Serial.write('\r');
}
    }

    if (Serial.available() > 0) {
wifly.write(Serial.read());
    }
}

/* Connect the WiFly serial to the serial monitor. */
void terminal()
{
    while (1) {
if (wifly.available() > 0) {
    Serial.write(wifly.read());
}

if (Serial.available() > 0) {
    wifly.write(Serial.read());
}
    }
}

When i run this code with HTTP/1.0 i have this error

Code: [Select]
16:19:04.509 -> open http://a************************5f.mock.pstmn.io 80
16:19:04.741 -> Connected to http://******************5f.mock.pstmn.io
16:19:04.907 -> HTTP/1.1 401 Unauthorized
16:19:04.940 ->
Access-Control-Allow-Origin: *
16:19:04.974 ->
Content-Type: application/json; charset=utf-8
16:19:05.040 ->
Date: Tue, 17 Sep 2019 14:19:05 GMT
16:19:05.073 ->
ETag: W/"63-NBrQ********************mc"
16:19:05.107 ->
Server: nginx
16:19:05.140 ->
Vary: Accept-Encoding
16:19:05.174 ->
x-srv-span: v=1;s=f1********23b
16:19:05.208 ->
x-srv-trace: v=1;t=2af********6
16:19:05.242 ->
Content-Length: 99
16:19:05.275 ->
Connection: Close
16:19:05.275 ->

16:19:05.275 ->
{"error":{"name":"invalidCredentialsError","message":"Unable to authenticate using access token."}}


Instead if i set HTTP/1.1 i have this other error

Code: [Select]
18:15:59.170 -> DeviceID: Wifly-WebClient
18:16:00.307 -> open http://a*******************************5f.mock.pstmn.io 80
18:16:00.507 -> Connected to http://a**************************f.mock.pstmn.io
18:16:00.673 -> HTTP/1.1 400 BAD_REQUEST
18:16:00.707 ->
Content-Length: 0
18:16:00.707 ->
Connection: Close



Please help me, thanks you guys.

vaj4088

It seems unlikely that the code should have asterisks in so many places.  What does your server expect?

TheAssalt00

The code has asterisks because i have censored my personal information liks wifi ssid, wifi password and the server links.

lesept

Either the URL of the site is incorrect or your credentials are wrong
Googling for "Unable to authenticate using access token" will show you that this error sometimes occured with Postman but the problem was fixed.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

pourduino

#4
Sep 18, 2019, 07:24 pm Last Edit: Sep 18, 2019, 07:32 pm by pourduino
The library you are using seems to be sending raw HTTP requests. i.e the only thing you are providing the server is "GET /test HTTP/1.1". Some servers give you a "Bad Request" error when you do this.

When you browse websites with an actual browser a lot more is sent to the server. This is an example for this site:
Code: [Select]

Host: forum.arduino.cc
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0 Waterfox/56.2.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://forum.arduino.cc/index.php?board=4.20
Cookie: ****
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0


These are called HTTP headers. Not all of them are essential tho. Try sending these lines one by one after sending your "GET" request:
Code: [Select]

Host: *******.mock.pstmn.io
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Connection: close


Like this:
Code: [Select]

/* Send the request */
wifly.println("GET /test HTTP/1.1");
wifly.println("Host: *******.mock.pstmn.io");
wifly.println("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");
wifly.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
wifly.println("Connection: close");
wifly.println();

If this didn't work either you have to open the URL in your browser and use the developer tools of your browser to see the actual HTTP headers that are being sent and by trial and error find out which ones stop the "Bad Request" error.

Go Up