Particle REST APİ

Hi…
i have an REST api (on Django app. that not important). and i have an domain on heroku. i think i should be able to send an data (ex: variable) from photon to my rest api (as json). the algorithm is good. send from particle and should my rest app receive it. but dose not work :smiley:
First my particle code:

    /* RestClient simple GET request
     *
     * credit: Chris Continanza (csquared)
     */

    #include "rest_client.h"

    RestClient client = RestClient("https://staj-photon.herokuapp.com/sensors/");

    //RestClient client = RestClient("127.0.0.1", 8000);

    String response;
    int statusCode;

    void setup() {
      Serial.begin(9600);
      Serial.println("starting...");

      while(!Serial.available()) Spark.process();
}

    void loop() {
        response = "";
        statusCode = client.post("/sensors", "id=9&user_And_Owner_Sensors=5", &response);
        Serial.println("Status code from server: " + String(statusCode));
        Serial.println("Response body from server: " + response);
        delay(1000);
    }

my Django Rest code is probly ture:

@api_view(['GET', 'POST'])
def snippet_list(request):
    """
    List all snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = SensorInfo.objects.all()
        serializer = Api(snippets, many=True)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = Api(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 

what i need is this code be working :
statusCode = client.post("/sensors", "id=9&user_And_Owner_Sensors=5", &response);
look at second parameter i want to send tow value… “id” and user_And_Owner_Sensors. from photon to my REST (should be redy to GET that values).

EDİT: [SLOVED]:
references: http://codefrieze.blogspot.com.tr/2016/06/particle-photon-making-http-requests.html
if you will use domain not ip :
request.hostname =“your_server” Witout http://

The hostname is wrong.
http:// is not part of the host name but is the protocol prefix.
I also think that /sensors/ actually is not part of the hostname, but should rather go into the path


Update:
OP code has been updated since

2 Likes

not actually. i had tried witout http:// ore witout /sensors . but nothing.
note: i edit my code to :
http.get(request, response, headers);

in case
Serial.println(request.body);
am able to see my data from putty. but dont know why cant any thing to my domain.

As long you are not showing the exact code you are running I doubt others would be willing to chase for something that might well be a red herring after pointing out the obvious errors in your code.

You are also not disclosing what extra info request.response provides you with. If you want us to help, you need to be more forthcoming with your “debug” info.

BTW, are you sure you want a GET request and not a POST or PUT?


Update:
OP code has been updated since

1 Like

Firstly am sorry. my english is bad. so i edit my post agin. i wish it understandable.
Yes like you said i need Posting data from photon to my REST. my REST code is good. and ready to recevie / GET values. Not put. just POST .

One additional point is that secure HTTPS (which you didn’t have in your original post) is not natively supported, you’d need to incorporate one of the 3rd party libraries to add that to your project.

1 Like

am sorry… how you know is my https dose not supported.
and is that mean all codes has no error ?
well Would you recommend some 3rd party lib to me.

I know that the Particle Photon does not support HTTPS because that support is not impemented in the Particle firmware.
But no, this does not mean that your code is free of errors, but since this (and the ones mentioned earlier) is the biggest obstacle for your code there was little point to look any further.

There are two possible libraries to add SSL/TLS support for HTTPS
Wolfssl [version 0.0.2; documenting]
TlsTcpClient library on WebIDE published

3 Likes

Hi. i do a little research about SSL. actually i dont need https at the moment. i just need send data from photon to my domain. i tried “rest_client.h” libary. as showed in my firs question. but no data sendind. then i tried “HttpClient.h” libary as show down. but nothing…
#include “application.h”

// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>

unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
      { "Content-Type", "application/json" },
        { NULL, NULL } // NOTE: Always terminate headers will NULL

};

http_request_t request;
http_response_t response;

void setup() {
    Serial.begin(9600);
}

void loop() {
    if (nextTime > millis()) {
        return;
    }

    Serial.println();
    //Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.hostname = "http://www.mocky.io/v2/59804eab110000b2081cf940";//my page which there a rest app. an ready to get data
    request.port = 80;

    //NOTE: Cant do Get or post. 
    // Get request
    http.post(request, response, headers);
    
    Serial.println(response.status);
    Serial.println(request.body);
    
    nextTime = millis() + 1000;
}

I think there is little more than I can do since you keep ignoring my advice - look at your host name again!

1 Like

no am not ignoring your advice… i tried witout " http:// ". but no thing. So should i remove the part is coming afte “.com etc” .
ex:
look at this link please:
www.posttestserver.com/data/2017/08/01/09.02.061268099243

link is work. and if you look at it. you will see id and name as json data. if i post and data as {id=10, name=anas} should i remove “/data/2017/08/01/09.02.061268099243”.

If you tried without http:// why not post that version as you already have been told with it it’s plain out wrong?
You don’t even need to try that again - it’s just not going to work, full stop.

And also, why do you think?

after having read this

i said that before :sweat_smile:

[quote=“anasMansour, post:3, topic:34953”]
not actually. i had tried witout http:// ore witout /sensors . but nothing. [/quote]

i cant full stop. as i know ther are just tow lib to “POST” data from photon.
1- rest_client.h
2- httpClient.h
So i must POST from photon as soon as possible. i think rest_client just not support json data. so I guess i will use httpClient.
thanks.

You said but your code never showed - not even the latest version after being told how to go about it correctly.

1 Like

edited my post. with [sloved].
thank you. when remove http:// and add right path to my rest api Worked.

2 Likes