<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>HTTP GET request with parameters.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/54110/http-get-request-with-parameters</link><description>Dear Community; 
 
 I am trying to make an HTTP GET request based on this code . It works fine but I need to adapt that to fit it in my code. I need to pass some arguments to the request, and I don&amp;#39;t know-how. 
 The goal is to send a Telegram message</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 14 Nov 2019 09:58:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/54110/http-get-request-with-parameters" /><item><title>RE: HTTP GET request with parameters.</title><link>https://devzone.nordicsemi.com/thread/220037?ContentTypeID=1</link><pubDate>Thu, 14 Nov 2019 09:58:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:023462fe-976b-4863-ac5d-ef504718c997</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;HTTPS is only HTTP on top of TLS, instead of regular TCP.&lt;/p&gt;
&lt;p&gt;So the only changes you should need to get HTTPS instead of HTTP is to use secure sockets.&lt;/p&gt;
&lt;p&gt;For an example on how to do this, you can take a look at the mqtt_client_tls_connect function (which creates and connects a secure socket, there is not much MQTT specific going on in that function): &lt;a href="https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/master/subsys/net/lib/mqtt/mqtt_transport_socket_tls.c#L21"&gt;https://github.com/NordicPlayground/fw-nrfconnect-zephyr/blob/master/subsys/net/lib/mqtt/mqtt_transport_socket_tls.c#L21&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;In short, you need to specify the TLS version as the protocol when creating the socket and provide the sec_tag the certificates are stored in on the modem (and of course ensure that the certificates have been written to the modem).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: HTTP GET request with parameters.</title><link>https://devzone.nordicsemi.com/thread/219538?ContentTypeID=1</link><pubDate>Mon, 11 Nov 2019 18:05:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3ac3f072-770a-41f7-9315-5f376c13928f</guid><dc:creator>isurki</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The problem was that the GET requests was bad, here you have the working one:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;quot;GET /botMYBOTTOKEN/sendMessage?chat_id=MYCHATID&amp;amp;text=MYMESSAGE HTTP/1.1\r\nHost: api.telegram.org \r\n\r\n&amp;quot;;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;However, I have reminded that httpS protocol is mandatory to use the API, and I can&amp;#39;t find any working example.&amp;nbsp; Currently, this is my code:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;gps.h&amp;gt;
#include &amp;lt;sensor.h&amp;gt;
#include &amp;lt;console.h&amp;gt;
#include &amp;lt;dk_buttons_and_leds.h&amp;gt;
#include &amp;lt;lte_lc.h&amp;gt;
#include &amp;lt;misc/reboot.h&amp;gt;
#include &amp;lt;net/bsdlib.h&amp;gt;
#include &amp;lt;net/socket.h&amp;gt;

#define HTTP_HOST &amp;quot;api.telegram.org&amp;quot;
#define HTTP_PORT 80
#define RECV_BUF_SIZE 8192

char recv_buf[RECV_BUF_SIZE + 1];

int blocking_recv(int fd, u8_t *buf, u32_t size, u32_t flags)
{
	int err;

	do {
		err = recv(fd, buf, size, flags);
	} while (err &amp;lt; 0 &amp;amp;&amp;amp; errno == EAGAIN);

	return err;
}

int blocking_send(int fd, u8_t *buf, u32_t size, u32_t flags)
{
	int err;

	do {
		err = send(fd, buf, size, flags);
	} while (err &amp;lt; 0 &amp;amp;&amp;amp; errno == EAGAIN);

	return err;
}

int blocking_connect(int fd, struct sockaddr *local_addr, socklen_t len)
{
	int err;

	do {
		err = connect(fd, local_addr, len);
	} while (err &amp;lt; 0 &amp;amp;&amp;amp; errno == EAGAIN);

	return err;
}

void app_http_start(void)
{
	struct sockaddr_in local_addr;
	struct addrinfo *res;

	local_addr.sin_family = AF_INET;
	local_addr.sin_port = htons(0);
	local_addr.sin_addr.s_addr = 0;

	printk(&amp;quot;HTTP example\n\r&amp;quot;);

	int err = getaddrinfo(HTTP_HOST, NULL, NULL, &amp;amp;res);

	/*printk(&amp;quot;getaddrinfo err: %d\n\r&amp;quot;, err);*/
	((struct sockaddr_in *)res-&amp;gt;ai_addr)-&amp;gt;sin_port = htons(HTTP_PORT);

    char send_buf[] = &amp;quot;GET /botMYBOTTOKEN/sendMessage?chat_id=MYCHATID&amp;amp;text=test HTTP/1.1\r\nHost: api.telegram.org\r\n\r\n&amp;quot;;
	
	int send_data_len = strlen(send_buf);

	int client_fd = socket(AF_INET, SOCK_STREAM, 0);

	printk(&amp;quot;client_fd: %d\n\r&amp;quot;, client_fd);
	err = bind(client_fd, (struct sockaddr *)&amp;amp;local_addr,
		   sizeof(local_addr));
	printk(&amp;quot;bind err: %d\n\r&amp;quot;, err);
	err = blocking_connect(client_fd, (struct sockaddr *)res-&amp;gt;ai_addr,
			       sizeof(struct sockaddr_in));
	printk(&amp;quot;connect err: %d\n\r&amp;quot;, err);

	int num_bytes = send(client_fd, send_buf, send_data_len, 0);

	printk(&amp;quot;send err: %d\n\r&amp;quot;, num_bytes);

	int tot_num_bytes = 0;

	do {
		/* TODO: make a proper timeout *
		 * Current solution will just hang 
		 * until remote side closes connection */
		num_bytes =
			blocking_recv(client_fd, recv_buf, RECV_BUF_SIZE, 0);
		tot_num_bytes += num_bytes;

		if (num_bytes &amp;lt;= 0) {
			break;
		}
		printk(&amp;quot;\n\nhere:%s\n\n&amp;quot;, recv_buf);
	} while (num_bytes &amp;gt; 0);

	printk(&amp;quot;\n\rFinished. Closing socket\n&amp;quot;);
	freeaddrinfo(res);
	err = close(client_fd);
}

static void modem_configure(void)
{
	if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) {
		/* Do nothing, modem is already turned on and connected. */
	} else {
		int err;

		printk(&amp;quot;Establishing LTE link (this may take some time) ...\n&amp;quot;);
		err = lte_lc_init_and_connect();
		__ASSERT(err == 0, &amp;quot;LTE link could not be established.&amp;quot;);
		printk(&amp;quot;LTE connection established.\n&amp;quot;);
	}
}


void main(void)
{
	int err;
	printk(&amp;quot;Application started\n&amp;quot;);
    k_sleep(2000);
    modem_configure();
    app_http_start();
	k_sleep(2000);
	if ( (err = lte_lc_offline() ) )
	{
	    printk( &amp;quot;Failure turning off Modem: %d\n&amp;quot;, err );
	}
	else{
	    printk(&amp;quot;Modem turned off.\n&amp;quot;);
}


}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Is there any way to implement HTTPS protocol in this code?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: HTTP GET request with parameters.</title><link>https://devzone.nordicsemi.com/thread/219087?ContentTypeID=1</link><pubDate>Thu, 07 Nov 2019 14:10:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8349b7b9-f571-42a8-a366-c4558062f8fd</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi.&lt;/p&gt;
&lt;p&gt;Are you certain that the &amp;quot;https://&amp;quot; should be part of the hostname?&lt;/p&gt;
&lt;p&gt;We have a simple (unofficial) HTTP sample here: &lt;a href="https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/http"&gt;https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples/samples/nrf9160/http&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We also have some old threads that might help you on your way:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/support-private/support/238303"&gt;https://devzone.nordicsemi.com/support-private/support/238303&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/46086/nrf9160-dk-http-post-to-my-webserver/182163#182163"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/46086/nrf9160-dk-http-post-to-my-webserver/182163#182163&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/46094/how-to-make-http-post-no-libcurl"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/46094/how-to-make-http-post-no-libcurl&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Didrik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>