Skip to content

Commit 79c49b9

Browse files
author
Shashi Ranjan
committed
okhttp post need body or null to be passed
1 parent 03b6f6a commit 79c49b9

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/targets/java/okhttp.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ module.exports = function (source, options) {
2020

2121
var code = new CodeBuilder(opts.indent)
2222

23-
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS' ]
23+
var methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD']
24+
25+
var methodsWithBody = [ 'POST', 'PUT', 'DELETE', 'PATCH']
2426

2527
code.push('OkHttpClient client = new OkHttpClient();')
2628
.blank()
@@ -34,6 +36,8 @@ module.exports = function (source, options) {
3436
code.push('RequestBody body = RequestBody.create(mediaType, %s);', JSON.stringify(source.postData.text))
3537
}
3638

39+
console.log(source)
40+
3741
code.push('Request request = new Request.Builder()')
3842
code.push(1, '.url("%s")', source.fullUrl)
3943
if (methods.indexOf(source.method.toUpperCase()) === -1) {
@@ -46,7 +50,12 @@ module.exports = function (source, options) {
4650
if (source.postData.text) {
4751
code.push(1, '.%s(body)', source.method.toLowerCase())
4852
}else {
49-
code.push(1, '.%s()', source.method.toLowerCase())
53+
console.log(methodsWithBody.indexOf(source.method.toUpperCase()))
54+
if (methodsWithBody.indexOf(source.method.toUpperCase()) >= 0) {
55+
code.push(1, '.%s(null)', source.method.toLowerCase())
56+
}else {
57+
code.push(1, '.%s()', source.method.toLowerCase())
58+
}
5059
}
5160
}
5261

test/fixtures/output/java/okhttp/cookies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Request request = new Request.Builder()
44
.url("http://mockbin.com/har")
5-
.post()
5+
.post(null)
66
.addHeader("cookie", "foo=bar; bar=baz")
77
.build();
88

0 commit comments

Comments
 (0)