Hello,

I am trying to POST the sensor data to a cloud platform by using the Serval HTTP client. Unfortunately I cannot use the Serval REST client because the maximum body size is way to small for my use case.

I used the XDK HTTP guide to get it working by using the IP address and port of my service using this code:

void sendPostRequest(char body[]) {
	Ip_Address_T destAddr;

	Ip_convertOctetsToAddr(192, 168, 1, 42, &destAddr);
	Ip_Port_T port = Ip_convertIntToPort(8080);

	Msg_T* msg_ptr;
	HttpClient_initRequest(&destAddr, port, &msg_ptr);
	HttpMsg_setReqMethod(msg_ptr, Http_Method_Post);
	HttpMsg_setReqUrl(msg_ptr, "/sensordata");
	HttpMsg_setContentType(msg_ptr, "application/json");

	Msg_prependPartFactory(msg_ptr, &writeNextPartToBuffer);

	// Send the request
	static Callable_T sentCallable;
	Callable_assign(&sentCallable, &onHTTPRequestSent);
	HttpClient_pushRequest(msg_ptr, &sentCallable, &onHTTPResponseReceived);
}

 

My problem is, that when I deploy my target service to a cloud platform I do not have the IP address of this service. I only have a subdomain name.

The HTTP guide says

> The HttpExampleClient example project contains code to resolve a host name to an IP address using the PAL module.

Which I belive is not possible in my case because the service does not have an exposed IP address.

Is there any way to send a POST request to my server using the host name?

 

Regards Marcel

0 (0 Votes)
RE: Send POST request to server via host name
Answer
11/7/17 4:20 PM as a reply to Marcel Bochtler.

Hello Marcel,

in Workbench versions 3.0, the host-header can be set using the function

HttpMsg_setHost(msg_ptr, "your.host.address");

additionally, with

Ip_Address_T ip;
PAL_getIpaddress((uint8_t *) "your.host.address", &ip);

you can resolve an address to its IP, which will then be stored in the variable ip of type Ip_Address_T. This can subsequentially be used as the destination address in HttpClient_initRequest(). Note that this function can also resolve an IP address, if its put in as a string. In that sense, it has double use as a ip-address-string-to-ip-address converter (but only if the WiFi Module is initialized)

Would this work for you? I think these are the only options, in regards to addressing virtual services with no own IP. From the perspective of the Cloud Platform Provider, I assume that in this case, the server should be able to find the correct service by looking at the host-header, while the HTTP request itself is sent to the Cloud Platform Provider's address.

If this does not work, could you tell me in more detail how the service should be addressed? (In general, not neccessarily in regards to the XDK, so I can check if there is an equivalent way on the XDK)

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Franjo

0 (0 Votes)
RE: Send POST request to server via host name
Answer
11/7/17 10:09 PM as a reply to Franjo Stjepandic.

Hi Franjo,

thanks for your answer. Unfortunately it is not working.

> in Workbench versions 3.0

Not sure why the workbench version should matter here, but anyway I'm using 3.0.1

I changed my code to this:

void sendPostRequest(char body[]) {
	Ip_Address_T destAddr;

	PAL_getIpaddress((uint8_t*) "http://subdomain.mydomain.com", &destAddr);
	Ip_Port_T port = Ip_convertIntToPort(80);

	Msg_T* msg_ptr;
	HttpMsg_setHost(msg_ptr, "subdomain.mydomain.com");
	HttpClient_initRequest(&destAddr, port, &msg_ptr);
	HttpMsg_setReqMethod(msg_ptr, Http_Method_Post);
	HttpMsg_setReqUrl(msg_ptr, "/sensordata");
	HttpMsg_setContentType(msg_ptr, "application/json");

	Msg_prependPartFactory(msg_ptr, &writeNextPartToBuffer);

	// Send the request
	static Callable_T sentCallable;
	Callable_assign(&sentCallable, &onHTTPRequestSent);

	HttpClient_pushRequest(msg_ptr, &sentCallable, &onHTTPResponseReceived);
}

 

I can reach my service e.g. with this CURL:

curl -X POST http://subdomain.mydomain.com/sensordata \
  -H 'content-type: application/json' \
  -d '{
  "lightSensorData": {
    "value": 23040,
    "unit": "mlux"
  },
  ......
}
'

 

As I understood you there is no way to reach my service directly via host name.

Because it is my own server and my own OpenShift instance, there might be a possibility that this can be solved on server side, but I really don't want to do this, because I'll test this use case on many different cloud platforms and I do not want to have the same problem on every one of them.

 

Edit: I'm getting the a retcode_t of 1357 in this line:

HttpClient_pushRequest(msg_ptr, &sentCallable, &onHTTPResponseReceived);

0 (0 Votes)
RE: Send POST request to server via host name
Answer
11/8/17 4:56 PM as a reply to Marcel Bochtler.

Hello Marcel,

the function PAL_getIpAddres() is a function that utilizes the DNS capability of the TI CC3100 WiFi chip. Since it is a DNS related function, it does not recognize the prefix http:// - it is simply not needed there, and in fact, it will throw the error message

5 [PIP:2] error in Get host IP by name -172

in the console log. Would removing this prefix in your call of PAL_getIpAddress() solve your issue? I slightly misunderstood your problem before, since I did not know how the server behaves. But this makes it clearer. Setting the host-header would not be neccessary then, either. But I recommend keeping that setting in, if it would not work without it.

Please tell me if this was helpful, and do not hesitate to ask further questions

Kind regards,
Franjo

0 (0 Votes)
RE: Send POST request to server via host name
Answer
11/10/17 10:43 AM as a reply to Franjo Stjepandic.

Hello Franjo,

I think I found the problem. Your suggestion to set the host header using:

HttpMsg_setHost(msg_ptr, "your.host.address");

was correct.

Unfortunately this is not working. I used wireshark to see what is actually sent and the host header is not beeing set to my virtual host (See Screenshot).

In another post you answered that this was a known issue. Should this be fixed by now?

https://xdk.bosch-connectivity.com/community/-/message_boards/message/276383

Edit:

Found it in the change log for the SDK that since 3.0.0 HttpMsg_setHost should work for my use case.

But it looks like it is not working.

Regards,

Marcel

0 (0 Votes)
RE: Send POST request to server via host name
Answer
11/10/17 3:20 PM as a reply to Marcel Bochtler.

Hello Marcel,

I have tried out the Host-Header in XDK-Workbench version 3.0.1, using the HttpExampleClient from the Welcome-Screen. I get the following wireshark log entry on my Raspberry (which I use as the WiFi access point for the XDK):

GET / HTTP/1.1
Content-Type: text/plain
Content-Length: 0
Host: test
Range: bytes=0-511

As this message is this message is originally sent from my XDK and forwarded by the RaspberryPi as is, this definitely works. Wondering why it does not work for you, I realized that firstly, the message in the wireshark entry, which you attached, looks like a message generated by the REST API. The REST API uses Transfer-Encoding: chunked, while the HTTP API does not. As such, make sure that you flashed the correct application onto the XDK.

Secondly, in the last code-snippet you posted, the function HttpMsg_setHost() is called before HttpClient_initRequest(). This will definitely not work, as the header should be set after the request has been initialized.

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Franjo

+1 (1 Vote)
RE: Send POST request to server via host name
Answer
11/10/17 3:47 PM as a reply to Franjo Stjepandic.

Hi Franjo.

the problem was that I was trying to set the host header before initializing the request. Thank you for that hint :)

Moved it one line down and now it is working.

Regards,

Marcel

0 (0 Votes)
RE: Send POST request to server via host name
Answer
11/13/17 5:14 PM as a reply to Marcel Bochtler.
Hello Marcel,

I am glad to hear that you were able to solve the issue.

Please do not hesitate to ask if you have further questions or concerns.

Kind regards,
Franjo
0 (0 Votes)