Java / Json-如何在HttpPost请求标头中添加/传递ApiKey?

时间:2018-08-10 03:21:23

标签: java json http-post api-key request-headers

我不想使用request.setEntity()方法传递apiKey。我只想在请求标头中传递它。如果将标题命名为“授权”是否正确?如果我做错了,请纠正我。

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(restAPIServiceURL);
request.addHeader("Content-Type", "application/json");
request.addHeader("Accept","application/json");
request.addHeader("Authorization","apiKey=AIzaSyCXhu........");
request.setEntity(new StringEntity(jsonString)); //I have other data to pass as Entity.

HttpResponse response = httpClient.execute(request);

还有其他更好的方法可以在请求标头中传递apiKey吗?

1 个答案:

答案 0 :(得分:0)

是的。你可以用这个

String API_KEY= "YOUR KEY"; String basicAuth = Basic + new String(Base64.encode(API_KEY.getBytes(), Base64.DEFAULT)); request.addHeader("Authorization", basicAuth);