MCPcopy Create free account
hub / github.com/SmingHub/Sming / connectOk

Function connectOk

samples/HttpClient/app/application.cpp:85–153  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

83}
84
85void connectOk(IpAddress ip, IpAddress mask, IpAddress gateway)
86{
87 Serial << _F("Connected. Got IP: ") << ip << endl;
88
89 // [ GET request: The example below shows how to make HTTP requests ]
90
91 // First: The HttpRequest object contains all the data that needs to be sent
92 // to the remote server.
93
94 HttpRequest* getRequest = new HttpRequest(F("https://httpbin.org/get"));
95 getRequest->setMethod(HTTP_GET); // << you may set the method. If not set the default HTTP_GET method will be used
96
97 // Headers: if you need to set custom headers then you can do something like ...
98 HttpHeaders headers;
99 headers[HTTP_HEADER_USER_AGENT] = _F("HttpClient/Sming"); // Prefer use of enumerated type for standard field names
100 headers[F("X-Powered-By")] = _F("Sming"); // Use text for non-standard field names
101 getRequest->setHeaders(headers);
102 // ... or like
103 getRequest->setHeader(F("X-Powered-By"), F("Sming"));
104
105 /*
106 * Notice: If we use SSL we need to set the SSL settings only for the first request
107 * and all consecutive requests to the same host:port will try to reuse those settings
108 */
109 getRequest->onSslInit(sslRequestInit);
110
111 // If we want to process the response we can do it by setting a onRequestCallback
112 getRequest->onRequestComplete(onDownload);
113
114 // Second: We have to send that request using our httpClient
115 httpClient.send(getRequest);
116
117 // [ POST request: the example below shows how to set a POST request with form data and files. ]
118 FileStream* fileStream = new FileStream("5K.txt");
119
120 HttpRequest* postRequest = new HttpRequest(F("https://httpbin.org/post"));
121 // For this request we will use a slightly improved syntax
122 postRequest
123 ->setMethod(HTTP_POST) // << we set the method to POST
124 ->setHeaders(headers) // << we add extra headers
125 ->setPostParameter("text", "Test upload") // << we set one form element called "text"
126 ->setFile("file1", fileStream) // << we set one file upload that should upload the data.txt
127 // ... under the form element name "file1"
128 ->onRequestComplete(onDownload);
129
130 httpClient.send(postRequest); // << don't forget to `send` the request
131
132 // [PUT request with raw data: We will send the data.txt content without any additional content encoding ]
133 FileStream* fileStream1 = new FileStream("20K.txt");
134
135 HttpRequest* putRequest = new HttpRequest(F("https://httpbin.org/put"));
136 putRequest->setMethod(HTTP_PUT)
137 ->setBody(fileStream1) // << we set the complete HTTP body
138 ->onRequestComplete(onDownload);
139
140 // Remark: Response body handling.
141 // The remote server will "echo" the data meaning that the body will be bigger than 20K.
142 // By default the http client will store in memory up to 1024 bytes.

Callers

nothing calls this directly

Calls 10

setHeadersMethod · 0.80
onSslInitMethod · 0.80
onRequestCompleteMethod · 0.80
setFileMethod · 0.80
setPostParameterMethod · 0.80
FFunction · 0.50
setMethodMethod · 0.45
setHeaderMethod · 0.45
sendMethod · 0.45
setBodyMethod · 0.45

Tested by

no test coverage detected