<?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>nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/92042/nrf9160-dk-http-post-request</link><description>Hi! We are trying to send an HTTP request to our AWS Web Server. The problem that we are figuring out now is that we don&amp;#39;t know the way to add a body to our request. We based our code on the Connection Example provided by Nordic ( https://developer.nordicsemi</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 22 Sep 2022 08:33:58 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/92042/nrf9160-dk-http-post-request" /><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/387423?ContentTypeID=1</link><pubDate>Thu, 22 Sep 2022 08:33:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f37fa6f-4ba6-4ce5-b562-47c844c3f5d6</guid><dc:creator>Gioele</dc:creator><description>&lt;p&gt;Probably the server wasn&amp;#39;t running but now everything is fine. We understood that the solution is the &amp;quot;/&amp;quot; that you mentioned plus the correct &amp;quot;content-length&amp;quot; of the body. Thank you!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/387336?ContentTypeID=1</link><pubDate>Wed, 21 Sep 2022 14:41:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9a78ec6-277c-4023-b294-a6988374a044</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Is the server running?&lt;/p&gt;
&lt;p&gt;I am trying to send the request myself, but the connection to the server times out.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/387133?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2022 14:39:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7c8bade4-431a-4546-bb56-31c24c2a0fef</guid><dc:creator>Gioele</dc:creator><description>&lt;p&gt;We tried to add a &amp;quot;/&amp;quot; but we have the same error. This is the exact request that we make on Postman.&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1663684744545v1.jpeg" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/387130?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2022 14:32:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c98d1d2c-d8fe-4b26-86e6-99817aaac102</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;No, that is the hostname. You also need the path to the resource you want to get/modify.&lt;/p&gt;
&lt;p&gt;Try add a &amp;#39;/&amp;#39; between the request method and the protocol version (separated by spaces), if you want&amp;nbsp; to modify the root object.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/387119?ContentTypeID=1</link><pubDate>Tue, 20 Sep 2022 14:10:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a582d61a-d4a1-4cf1-995c-a712e33c5987</guid><dc:creator>Gioele</dc:creator><description>&lt;p&gt;This is the&amp;nbsp;URL #define HTTPS_HOSTNAME &amp;quot;ec2-184-73-45-204.compute-1.amazonaws.com&amp;quot;&lt;br /&gt;&lt;br /&gt;If I make the same request on PostMan it runs!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/386875?ContentTypeID=1</link><pubDate>Mon, 19 Sep 2022 12:39:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f363cb0f-cbca-4457-984b-5a60e4ec3a4a</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Where?&lt;/p&gt;
&lt;p&gt;I still don&amp;#39;t see any URL between the request method (POST) and the protocol version (HTTP/1.1)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/386863?ContentTypeID=1</link><pubDate>Mon, 19 Sep 2022 12:24:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1508404e-4b54-4dff-83ff-b4fda7b19654</guid><dc:creator>Gioele</dc:creator><description>&lt;p&gt;The URL is included!&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define HTTPS_PORT 8545

#define HTTPS_HOSTNAME &amp;quot;ec2-184-73-45-204.compute-1.amazonaws.com&amp;quot;
#define BODY  &amp;quot;{ \&amp;quot;jsonrpc\&amp;quot;: \&amp;quot;2.0\&amp;quot;, \&amp;quot;method\&amp;quot;: \&amp;quot;eth_chainId\&amp;quot; }&amp;quot;



#define HTTP_HEAD &amp;quot;POST HTTP/1.1\r\n&amp;quot;\
		&amp;quot;Host: &amp;quot;HTTPS_HOSTNAME&amp;quot;\r\n&amp;quot;\
		&amp;quot;Connection: close\r\n&amp;quot;\
	    &amp;quot;Content-type: application/json\r\n&amp;quot;\
		&amp;quot;\r\n&amp;quot;\
		&amp;quot;&amp;quot;BODY&amp;quot;\r\n\r\n&amp;quot;

void main(void)
{
	int err;
	int fd;
	char *p;
	int bytes;
	size_t off;
	struct addrinfo *res;
	struct addrinfo hints = {
		.ai_family = AF_INET,
		.ai_socktype = SOCK_STREAM,
	};

	printk(&amp;quot;HTTPS client sample started\n&amp;quot;);

// #if !defined(CONFIG_SAMPLE_TFM_MBEDTLS)
// 	/* Provision certificates before connecting to the LTE network */
// 	err = cert_provision();
// 	if (err) {
// 		return;
// 	}
// #endif

	printk(&amp;quot;Waiting for network.. \n&amp;quot;);
	err = lte_lc_init_and_connect();
	if (err) {
		printk(&amp;quot;Failed to connect to the LTE network, err %d\n&amp;quot;, err);
		return;
	}
	printk(&amp;quot;OK\n&amp;quot;);

	err = getaddrinfo(HTTPS_HOSTNAME, NULL, &amp;amp;hints, &amp;amp;res);
	if (err) {
		printk(&amp;quot;getaddrinfo() failed, err %d\n&amp;quot;, errno);
		return;
	}

	((struct sockaddr_in *)res-&amp;gt;ai_addr)-&amp;gt;sin_port = htons(HTTPS_PORT);

	if (IS_ENABLED(CONFIG_SAMPLE_TFM_MBEDTLS)) {
		fd = socket(AF_INET, SOCK_STREAM | SOCK_NATIVE_TLS, IPPROTO_TCP);
	} else {
		fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	}
	if (fd == -1) {
		printk(&amp;quot;Failed to open socket!\n&amp;quot;);
		goto clean_up;
	}

	// /* Setup TLS socket options */
	// err = tls_setup(fd);
	// if (err) {
	// 	goto clean_up;
	// }

	printk(&amp;quot;Connecting to %s\n&amp;quot;, HTTPS_HOSTNAME);
	err = connect(fd, res-&amp;gt;ai_addr, sizeof(struct sockaddr_in));
	if (err) {
		printk(&amp;quot;connect() failed, err: %d\n&amp;quot;, errno);
		goto clean_up;
	}

	off = 0;
	do {
		bytes = send(fd, &amp;amp;send_buf[off], HTTP_HEAD_LEN - off, 0);
		if (bytes &amp;lt; 0) {
			printk(&amp;quot;send() failed, err %d\n&amp;quot;, errno);
			goto clean_up;
		}
		off += bytes;
		printk(&amp;quot;\n&amp;gt;\t %s\n\n&amp;quot;, send_buf);


	} while (off &amp;lt; HTTP_HEAD_LEN);

	printk(&amp;quot;Sent %d bytes\n&amp;quot;, off);


	off = 0;
	do {
		bytes = recv(fd, &amp;amp;recv_buf[off], RECV_BUF_SIZE - off, 0);
		if (bytes &amp;lt; 0) {
			printk(&amp;quot;recv() failed, err %d\n&amp;quot;, errno);
			goto clean_up;
		}
		off += bytes;
		printk(&amp;quot;Sto ricevendo bytes\n&amp;quot;);
		printk(&amp;quot;%d bytes\n&amp;quot;, bytes);

	} while (bytes != 0 /* peer closed connection */);

	printk(&amp;quot;Received %d bytes\n&amp;quot;, off);

	/* Print HTTP response */
	p = strstr(recv_buf, &amp;quot;\r\n&amp;quot;);
	if (p) {
		off = p - recv_buf;
		recv_buf[off + 1] = &amp;#39;\0&amp;#39;;
		printk(&amp;quot;\n&amp;gt;\t %s\n\n&amp;quot;, recv_buf);
	}

	int tot_num_bytes = 0;

	printk(&amp;quot;Finished, closing socket.\n&amp;quot;);
	freeaddrinfo(res);

(void)close(fd);

	lte_lc_power_off();
	
clean_up:
	freeaddrinfo(res);
	(void)close(fd);

	lte_lc_power_off();
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/386783?ContentTypeID=1</link><pubDate>Mon, 19 Sep 2022 08:18:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c0242e4a-066e-44bd-a277-3ba9bfe9ebc1</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;I had another look at the request you are sending, and it does not look like you have the URL included in the request line.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/386731?ContentTypeID=1</link><pubDate>Sat, 17 Sep 2022 10:32:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9ee62142-5edd-42a6-aae1-0484d33feb1d</guid><dc:creator>Gioele</dc:creator><description>&lt;p&gt;The request is correct, we make the same request in Postman.&lt;br /&gt;When we try to print the request,&amp;nbsp;we do not always have the result, but when there is, it is correct. Could it be that there is another way to send&amp;nbsp;the body? It is a JSON.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/386599?ContentTypeID=1</link><pubDate>Fri, 16 Sep 2022 11:01:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:77ed585c-e079-4ba9-80d8-7148781ad5e1</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Then your problem is something else.&lt;/p&gt;
&lt;p&gt;Have you tried to print the request, to make sure it is what you want?&lt;/p&gt;
&lt;p&gt;Have you tried to send the same request from another client, to make sure that the request itself is okay?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/386594?ContentTypeID=1</link><pubDate>Fri, 16 Sep 2022 10:54:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bf67e704-8b7f-46b7-8a19-a1dc72da825d</guid><dc:creator>Gioele</dc:creator><description>&lt;p&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define HTTP_HEAD &amp;quot;POST HTTP/1.1\r\n&amp;quot;\
		&amp;quot;Host: &amp;quot;HTTPS_HOSTNAME&amp;quot;\r\n&amp;quot;\
		&amp;quot;Connection: close\r\n&amp;quot;\
	    &amp;quot;Content-type: application/json\r\n&amp;quot;\
		&amp;quot;\r\n&amp;quot;\
		&amp;quot;&amp;quot;BODY&amp;quot;\r\n\r\n&amp;quot;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Same problem!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF9160-DK HTTP POST REQUEST</title><link>https://devzone.nordicsemi.com/thread/386585?ContentTypeID=1</link><pubDate>Fri, 16 Sep 2022 10:25:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5b35e75-60df-4518-badf-93208c5d57d4</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The header is separated from the body with an empty line, consisting of a carriage return and a line feed.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#HTTP/1.1_request_messages"&gt;https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#HTTP/1.1_request_messages&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>