POST Request with HttpExampleClient
Antwort
12.05.16 13:01

Hi,

I want to send data via a HTTP Post. Therefore I want to adapt the HttpExampleClient, but I was not able to find out, how i put in my payload. I think it should have something to do with the variable "Msg_T* msg_ptr" which is used in multiple calls of the HttpClient. But as fas as I understand the struct represent not just my payload, but a whole messaging session.

Is there any documentation for the "ServalStack" or could somebody help me out?

 

Best regards Bernd

+1 (1 Stimme)
RE: POST Request with HttpExampleClient
Antwort
13.05.16 14:34 als Antwort auf Bernd Mertesz.

Hi Bernd,

 

the HttpExampleClient is definitly the right starting point for your purpose, but it only provides a GET request.

The Msg_T struct should include the complete content for the http request.

 

Unfortunately, there is no concrete example how to post data and the Serval_Http.h interface provides no function like HttpMsg_setContent(). I'm currently not sure how to set up this content too. 

But I'm looking forward to get a proper solution.

 

I'll keep you posted in this thread.

 

Kind regards,

Manuel

0 (0 Stimmen)
RE: POST Request with HttpExampleClient
Antwort
20.05.16 11:01 als Antwort auf Manuel Cerny.

Hi  Manuel,

is there any documentation for the struct Msg_T ?

If i want to use HTTPS instead of HTTP, is the only thing I need to is to change the method to HttpClient_initSecureRequest() ? Or is there anything I would need to do?

Do you know which specific crypto algorithmns are supported?

Kind regards,

Bernd

0 (0 Stimmen)
RE: POST Request with HttpExampleClient
Antwort
20.05.16 12:43 als Antwort auf Bernd Mertesz.

Hi Bernd,

 

sorry for letting you waiting, I’m still working on a solution for this.

Unfortunately there isn’t any detailed documentation about the Msg_T structure, and I know that there is space for improvement here.

 

I need to ask you for patience with HTTPS too. It is currently in a technical preview state and not supported right now.

 

Kind regards

Manuel

0 (0 Stimmen)
RE: POST Request with HttpExampleClient
Antwort
15.06.16 09:59 als Antwort auf Manuel Cerny.

Hi Bernd,

after some research and testing I have a solution for you.
The Serval stack isn't easy to handle and the content body can’t be configurated in a familiar way.

The Http API needs a function which is able to write the payload in a buffer.
This function needs to be declared in the .h file, that you can call it anywhere you need to in the implementation file:

In HttpExampleClient.h:
 

/* local inline function definitions */
retcode_t helloWorldSerializer(OutMsgSerializationHandover_T* handover);


Then you add the implementation in HttpExampleClient.c (I my case I did it at the end of the file):
 

retcode_t helloWorldSerializer(OutMsgSerializationHandover_T* handover) {

      retcode_t rc;
      char* payload = "Hello World";

      uint16_t entryLength = (uint16_t) strlen(payload);
      uint16_t alreadySerialized = handover->offset;
      uint16_t remainingLength = entryLength - alreadySerialized;
      uint16_t bytesToCopy;

      if ( remainingLength <= handover->bufLen ) {

          bytesToCopy = remainingLength;
          rc = RC_OK;
      }
      else {

          bytesToCopy = handover->bufLen;
          rc = RC_MSG_FACTORY_INCOMPLETE;
      }
      memcpy(handover->buf_ptr, payload + alreadySerialized, bytesToCopy);
            handover->offset = alreadySerialized + bytesToCopy;
            handover->len = bytesToCopy;

      if (handover->bufLen<strlen(payload)) return RC_MSG_FACTORY_INCOMPLETE;
      handover->len = sprintf(handover->buf_ptr, payload);
      return RC_OK;
}

After initializing the message you need to call the Msg_prepentPartFactory():

    rc = HttpClient_initRequest(&destAddr, Ip_convertIntToPort(destPort), &msg_ptr);

    if (rc != RC_OK || msg_ptr == NULL) {
        printf("Failed HttpClient_initRequest\r\n " );
        return;
    }
    rc = Msg_prependPartFactory( msg_ptr, &helloWorldSerializer);

    if (rc != RC_OK) {
         printf("Failed Msg_prependPartFactory \r\n  ");
         return;
    }
    rc = HttpMsg_setReqUrl(msg_ptr, url_ptr);

    if (rc != RC_OK) {
         printf("Failed to fill message \r\n ");
         return;

    }
    HttpMsg_setReqMethod(msg_ptr, Http_Method_Post);

    rc = HttpClient_pushRequest(msg_ptr, &SentCallable, &httpClientResponseCallback);


Please tell me if you get it working.

Kind regards,
Manuel

+1 (1 Stimme)
RE: POST Request with HttpExampleClient
Antwort
10.08.16 07:46 als Antwort auf Manuel Cerny.

Hello - would be great if we would have a big HTTP Request solution as we have made with MQTT and the PAHO Tutorial. We can call every sensor with an http get request and will get an answer in json format - this coud be a way.

You can see a JSON output of your XDK if you call like this

http://xdk-ip-goes-here/xdk-device-ID-goes-here/humidity?access_token=your-access-token-goes-here

Response
{"humidity": 43}

+2 (2 Stimmen)
RE: POST Request with HttpExampleClient
Antwort
10.08.16 09:17 als Antwort auf Achim Kern.

Hello Achim,

thank you very much for this hint!

I definitely agree with you. We need a tutorial or guide to explain HTTP requests in general. Maybe as you have mentioned with a REST interface.
I will add this to our improvement list and discuss it with the development team.

Please feel free to open new threads for new topics.

- Manuel

0 (0 Stimmen)