Skip to content
Permalink
Browse files
network: only set Content-type for JSON requests
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Apr 19, 2021
1 parent 9b9fe91 commit 63a0d5931b9716c27577c3a3ca081de857715cab
Showing with 12 additions and 5 deletions.
  1. +12 −5 network/src/main/java/org/openjdk/skara/network/RestRequest.java
@@ -97,6 +97,10 @@ private String composedBody() {
return finalBody.toString();
}

private boolean isJSON() {
return body != null || !bodyParams.isEmpty();
}

/**
* Pass a parameter through the url query mechanism.
* @param key
@@ -309,7 +313,7 @@ private Optional<JSONValue> transformBadResponse(HttpResponse<String> response,
}

private HttpRequest.Builder createRequest(RequestType requestType, String endpoint, String body,
List<QueryBuilder.Param> params, Map<String, String> headers) {
List<QueryBuilder.Param> params, Map<String, String> headers, boolean isJSON) {
var uriBuilder = URIBuilder.base(apiBase);
if (endpoint != null && !endpoint.isEmpty()) {
uriBuilder = uriBuilder.appendPath(endpoint);
@@ -321,8 +325,11 @@ private HttpRequest.Builder createRequest(RequestType requestType, String endpoi

var requestBuilder = HttpRequest.newBuilder()
.uri(uri)
.timeout(Duration.ofSeconds(30))
.header("Content-type", "application/json");
.timeout(Duration.ofSeconds(30));

if (isJSON) {
requestBuilder = requestBuilder.header("Content-type", "application/json");
}

if (body != null) {
requestBuilder.method(requestType.name(), HttpRequest.BodyPublishers.ofString(body));
@@ -373,7 +380,7 @@ private JSONValue combinePages(List<JSONValue> pages) {

private JSONValue execute(QueryBuilder queryBuilder) throws IOException {
var request = createRequest(queryBuilder.queryType, queryBuilder.endpoint, queryBuilder.composedBody(),
queryBuilder.params, queryBuilder.headers);
queryBuilder.params, queryBuilder.headers, queryBuilder.isJSON());
var response = sendRequest(request);
var errorTransform = transformBadResponse(response, queryBuilder);
if (errorTransform.isPresent()) {
@@ -414,7 +421,7 @@ private JSONValue execute(QueryBuilder queryBuilder) throws IOException {

private String executeUnparsed(QueryBuilder queryBuilder) throws IOException {
var request = createRequest(queryBuilder.queryType, queryBuilder.endpoint, queryBuilder.composedBody(),
queryBuilder.params, queryBuilder.headers);
queryBuilder.params, queryBuilder.headers, queryBuilder.isJSON());
var response = sendRequest(request);
if (response.statusCode() >= 400) {
throw new IOException("Bad response: " + response.statusCode());

1 comment on commit 63a0d59

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 63a0d59 Apr 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.