Skip to main content
1-Visitor
June 27, 2018
Solved

HTTP GET Request

  • June 27, 2018
  • 1 reply
  • 3088 views

Hi,

I am able to send my Sensor data to Thingworx using REST calls from ESP8266 ,

but not able to fetch a property value from Thingworx using GET request instead  getting 401 error(Unauthorised) 

even though there is correct appKey. I can fetch data using Python but in "C " am getting problem.

Please help me where i went wrong.

Thank you!

 

Here is the code:

 

 

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include<arduino.h>
#include <ESP8266HTTPClient.h>

#define ACCEPT_TYPE "application/json"


const char* ssid = "D-Link_DIR-615";
const char* password = "cadoptiot@123";


const char TWPlatformBaseURL[] = "http://xxx.ngrok.io:80";
const char appKey[] = "axxxxxxxxxxxxxxxxx8";


WiFiClient client;


void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());;

 

}

void loop()
{

HTTPClient http;
int httpCode = -1;
String response = "";
Serial.print("[httpGetPropertry] begin...");
String fullRequestURL = String(TWPlatformBaseURL) + "/Thingworx/Things/Capacity/Properties/hour?appKey=" + String(appKey);

http.begin(fullRequestURL);
http.addHeader("Accept",ACCEPT_TYPE,false,false);
Serial.println("GET URL>" + fullRequestURL +"<");
// start connection and send HTTP header
httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0)
{
response = http.getString();
Serial.printf("[httpGetPropertry] response code:%d body>",httpCode);
Serial.println(response + "<\n");
} else
{
Serial.printf("[httpGetPropertry] failed, error: %s\n\n", http.errorToString(httpCode).c_str());
}
http.end();

}

 

 

Best answer by vxavier

Hello,

 

Check the expiration time of your appKey(a lot of times I got this error because expiration time, and maybe when you did in python, it was still valid), and check the user permissions of that appKey, if he has the permissions to do what you are trying to do with this code.

 

Cheers,

Vinicius.

1 reply

1-Visitor
June 27, 2018

The Serial Monitor shows:

 

[httpGetPropertry] begin...GET URL>http://baz.ngrok.io:80/Thingworx/Things/Capacity/Properties/hour?appKey=a3188ba0-944a-486c-92a1-d434c6154898<
[httpGetPropertry] response code:401 body><

vxavier5-Regular MemberAnswer
5-Regular Member
June 27, 2018

Hello,

 

Check the expiration time of your appKey(a lot of times I got this error because expiration time, and maybe when you did in python, it was still valid), and check the user permissions of that appKey, if he has the permissions to do what you are trying to do with this code.

 

Cheers,

Vinicius.

1-Visitor
June 28, 2018

Hi,

    I have checked it properly , i have used same appkey for both Python and Arduino code

and i am administrator, i had all permissions i guess..

 

Thankyou!

Akshay