Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit c0ee9d6

Browse files
authored
Merge pull request #30 from iterate-ch/bugfix/POST-request-contenttype
Fix MIME in POST request.
2 parents 21ba881 + a2f2e0e commit c0ee9d6

1 file changed

Lines changed: 37 additions & 35 deletions

File tree

src/main/java/synapticloop/b2/request/BaseB2Request.java

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
/*
44
* Copyright (c) 2016 Synapticloop.
5-
*
5+
*
66
* All rights reserved.
7-
*
8-
* This code may contain contributions from other parties which, where
9-
* applicable, will be listed in the default build file for the project
7+
*
8+
* This code may contain contributions from other parties which, where
9+
* applicable, will be listed in the default build file for the project
1010
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
11-
*
12-
* This source code and any derived binaries are covered by the terms and
13-
* conditions of the Licence agreement ("the Licence"). You may not use this
14-
* source code or any derived binaries except in compliance with the Licence.
15-
* A copy of the Licence is available in the file named LICENSE.txt shipped with
11+
*
12+
* This source code and any derived binaries are covered by the terms and
13+
* conditions of the Licence agreement ("the Licence"). You may not use this
14+
* source code or any derived binaries except in compliance with the Licence.
15+
* A copy of the Licence is available in the file named LICENSE.txt shipped with
1616
* this source code or binaries.
1717
*/
1818

@@ -44,6 +44,8 @@
4444
import synapticloop.b2.exception.B2ApiException;
4545
import synapticloop.b2.response.B2AuthorizeAccountResponse;
4646

47+
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
48+
4749
public abstract class BaseB2Request {
4850
private static final Logger LOGGER = LoggerFactory.getLogger(BaseB2Request.class);
4951

@@ -85,7 +87,7 @@ protected BaseB2Request(CloseableHttpClient client, final String url) {
8587
/**
8688
* Instantiate the base B2 request which adds headers with the authorization
8789
* token.
88-
*
90+
*
8991
* @param client Shared HTTP client
9092
* @param b2AuthorizeAccountResponse the authorize account response
9193
* @param url Fully qualified request URI
@@ -97,7 +99,7 @@ protected BaseB2Request(CloseableHttpClient client, B2AuthorizeAccountResponse b
9799
/**
98100
* Instantiate the base B2 request which adds headers with the authorization
99101
* token.
100-
*
102+
*
101103
* @param client Shared HTTP client
102104
* @param b2AuthorizeAccountResponse the authorize account response
103105
* @param url Fully qualified request URI
@@ -113,7 +115,7 @@ protected BaseB2Request(CloseableHttpClient client, B2AuthorizeAccountResponse b
113115

114116
/**
115117
* Add header to request replacing previous if any
116-
*
118+
*
117119
* @param key the key to add
118120
* @param value the value to add
119121
*/
@@ -123,7 +125,7 @@ protected void addHeader(String key, String value) {
123125

124126
/**
125127
* Add query parameter to request replacing previous if any
126-
*
128+
*
127129
* @param key the key to add
128130
* @param value the value to add
129131
*/
@@ -133,7 +135,7 @@ protected void addParameter(String key, String value) {
133135

134136
/**
135137
* Add property to JSON request body
136-
*
138+
*
137139
* @param key the key to add
138140
* @param value the value to add
139141
*/
@@ -143,9 +145,9 @@ protected void addProperty(String key, Object value) {
143145

144146
/**
145147
* Execute an HTTP HEAD request and return the response for further parsing
146-
*
148+
*
147149
* @return the response object
148-
*
150+
*
149151
* @throws B2ApiException if something went wrong with the call
150152
* @throws IOException if there was an error communicating with the API service
151153
*/
@@ -173,7 +175,7 @@ protected CloseableHttpResponse executeHead() throws B2ApiException, IOException
173175

174176
/**
175177
* Execute a GET request, returning the data stream from the response.
176-
*
178+
*
177179
* @return The response from the GET request
178180
*
179181
* @throws B2ApiException if there was an error with the request
@@ -203,9 +205,9 @@ protected CloseableHttpResponse executeGet() throws B2ApiException, IOException
203205

204206
/**
205207
* Execute a POST request returning the response data as a String
206-
*
208+
*
207209
* @return the response data as a string
208-
*
210+
*
209211
* @throws B2ApiException if there was an error with the call, most notably
210212
* a non OK status code (i.e. not 200)
211213
* @throws IOException if there was an error communicating with the API service
@@ -216,7 +218,7 @@ protected CloseableHttpResponse executePost() throws B2ApiException, IOException
216218
String postData = convertPostData();
217219
HttpPost httpPost = new HttpPost(uri);
218220

219-
httpPost.setEntity(new StringEntity(postData));
221+
httpPost.setEntity(new StringEntity(postData, APPLICATION_JSON));
220222
CloseableHttpResponse httpResponse = this.execute(httpPost);
221223

222224
switch(httpResponse.getStatusLine().getStatusCode()) {
@@ -234,11 +236,11 @@ protected CloseableHttpResponse executePost() throws B2ApiException, IOException
234236

235237
/**
236238
* Execute a POST request with the contents of a file.
237-
*
239+
*
238240
* @param entity Content to write
239-
*
241+
*
240242
* @return the string representation of the response
241-
*
243+
*
242244
* @throws B2ApiException if there was an error with the call, most notably
243245
* a non OK status code (i.e. not 200)
244246
* @throws IOException if there was an error communicating with the API service
@@ -263,9 +265,9 @@ protected CloseableHttpResponse executePost(HttpEntity entity) throws B2ApiExcep
263265
/**
264266
* Convert the stringData and integerData Maps to JSON format, to be included
265267
* in the POST body of the request.
266-
*
268+
*
267269
* @return the JSON string of the data
268-
*
270+
*
269271
* @throws IOException if there was an error converting the data.
270272
*/
271273
protected String convertPostData() throws IOException {
@@ -283,11 +285,11 @@ protected String convertPostData() throws IOException {
283285
}
284286

285287
/**
286-
* Return the URI for this request, which adds any parameters found in the
288+
* Return the URI for this request, which adds any parameters found in the
287289
* 'parameters' data structure
288-
*
290+
*
289291
* @return The URI for this request, with properly encoded parameters
290-
*
292+
*
291293
* @throws IOException If there was an error building the URI
292294
*/
293295
protected URI buildUri() throws IOException {
@@ -306,15 +308,15 @@ protected URI buildUri() throws IOException {
306308
}
307309

308310
/**
309-
* Set the headers safely, go through the headers Map and add them to the http
310-
* request with properly encode values. If they already exist on the http
311+
* Set the headers safely, go through the headers Map and add them to the http
312+
* request with properly encode values. If they already exist on the http
311313
* request, it will be ignored.
312-
*
314+
*
313315
* To override what headers are set, this should be done in the constructor
314316
* of the base request object.
315-
*
317+
*
316318
* @param request The HTTP request to set the headers on
317-
*
319+
*
318320
* @throws B2ApiException if there was an error setting the headers
319321
*/
320322
protected void setHeaders(HttpUriRequest request) throws B2ApiException {
@@ -342,9 +344,9 @@ protected CloseableHttpResponse execute(final HttpUriRequest request) throws IOE
342344
/**
343345
* Obfuscate the data by removing the accountId and replacing it with the
344346
* string "[redacted]"
345-
*
347+
*
346348
* @param data the data to obfuscate
347-
*
349+
*
348350
* @return the obfuscated data
349351
*/
350352
private Object obfuscateData(String key, Object data) {

0 commit comments

Comments
 (0)