use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.
the class DefaultHttpClientRequest method build.
static HttpRequestBase build(URI uri, String method, RequestHttpEntity requestHttpEntity, RequestConfig defaultConfig) throws Exception {
final Header headers = requestHttpEntity.getHeaders();
final BaseHttpMethod httpMethod = BaseHttpMethod.sourceOf(method);
final HttpRequestBase httpRequestBase = httpMethod.init(uri.toString());
HttpUtils.initRequestHeader(httpRequestBase, headers);
if (MediaType.APPLICATION_FORM_URLENCODED.equals(headers.getValue(HttpHeaderConsts.CONTENT_TYPE)) && requestHttpEntity.getBody() instanceof Map) {
HttpUtils.initRequestFromEntity(httpRequestBase, (Map<String, String>) requestHttpEntity.getBody(), headers.getCharset());
} else {
HttpUtils.initRequestEntity(httpRequestBase, requestHttpEntity.getBody(), headers);
}
mergeDefaultConfig(httpRequestBase, requestHttpEntity.getHttpClientConfig(), defaultConfig);
return httpRequestBase;
}
use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.
the class NamingHttpUtilTest method testBuilderHeader.
@Test
public void testBuilderHeader() {
Header header = NamingHttpUtil.builderHeader();
Assert.assertNotNull(header);
Assert.assertEquals(header.getValue(HttpHeaderConsts.CLIENT_VERSION_HEADER), VersionUtils.version);
Assert.assertEquals(header.getValue(HttpHeaderConsts.USER_AGENT_HEADER), VersionUtils.getFullClientVersion());
Assert.assertEquals(header.getValue(HttpHeaderConsts.ACCEPT_ENCODING), "gzip,deflate,sdch");
Assert.assertEquals(header.getValue(HttpHeaderConsts.CONNECTION), "Keep-Alive");
Assert.assertNotNull(header.getValue(HttpHeaderConsts.REQUEST_ID));
Assert.assertEquals(header.getValue(HttpHeaderConsts.REQUEST_MODULE), "Naming");
}
use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.
the class ServerListManager method getServerListFromEndpoint.
private List<String> getServerListFromEndpoint() {
try {
String urlString = HTTP_PREFIX + endpoint + "/nacos/serverlist";
Header header = NamingHttpUtil.builderHeader();
Query query = StringUtils.isNotBlank(namespace) ? Query.newInstance().addParam("namespace", namespace) : Query.EMPTY;
HttpRestResult<String> restResult = nacosRestTemplate.get(urlString, header, query, String.class);
if (!restResult.ok()) {
throw new IOException("Error while requesting: " + urlString + "'. Server returned: " + restResult.getCode());
}
String content = restResult.getData();
List<String> list = new ArrayList<String>();
for (String line : IoUtils.readLines(new StringReader(content))) {
if (!line.trim().isEmpty()) {
list.add(line.trim());
}
}
return list;
} catch (Exception e) {
NAMING_LOGGER.error("[SERVER-LIST] failed to update server list.", e);
}
return null;
}
use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.
the class NotifyService method invokeURL.
/**
* Invoke http get request.
*
* @param url url
* @param headers headers
* @param encoding encoding
* @return {@link com.alibaba.nacos.common.model.RestResult}
* @throws Exception throw Exception
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public static RestResult<String> invokeURL(String url, List<String> headers, String encoding) throws Exception {
Header header = Header.newInstance();
header.addParam(HttpHeaderConsts.ACCEPT_CHARSET, encoding);
if (CollectionUtils.isNotEmpty(headers)) {
header.addAll(headers);
}
return HttpClientManager.getNacosRestTemplate().get(url, header, Query.EMPTY, String.class);
}
use of com.alibaba.nacos.common.http.param.Header in project nacos by alibaba.
the class HttpUtilsTest method testInitRequestEntity2.
@Test
public void testInitRequestEntity2() throws Exception {
BaseHttpMethod.HttpGetWithEntity httpRequest = new BaseHttpMethod.HttpGetWithEntity("");
Header header = Header.newInstance();
header.addParam(HttpHeaderConsts.CONTENT_TYPE, "text/html");
HttpUtils.initRequestEntity(httpRequest, Collections.singletonMap("k", "v"), header);
HttpEntity entity = httpRequest.getEntity();
InputStream contentStream = entity.getContent();
byte[] bytes = new byte[contentStream.available()];
contentStream.read(bytes);
Assert.assertEquals("{\"k\":\"v\"}", new String(bytes, Constants.ENCODE));
Assert.assertEquals(HttpHeaderConsts.CONTENT_TYPE, entity.getContentType().getName());
Assert.assertEquals("text/html; charset=UTF-8", entity.getContentType().getValue());
}
Aggregations