Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

network: only set Content-type for JSON requests #1124

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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());