11/**
22 * @description
3- * HTTP code snippet generator for Java using Unirest .
3+ * HTTP code snippet generator for Java using OkHttp .
44 *
55 * @author
66 * @shashiranjan 84
@@ -21,24 +21,31 @@ module.exports = function (source, options) {
2121 var code = new CodeBuilder ( opts . indent )
2222
2323 var methods = [ 'GET' , 'POST' , 'PUT' , 'DELETE' , 'PATCH' , 'HEAD' , 'OPTIONS' ]
24-
24+
2525 if ( methods . indexOf ( source . method . toUpperCase ( ) ) === - 1 ) {
26- return 'Method not supported'
26+ return 'Method not supported'
2727 }
28-
28+
29+ code . push ( 'OkHttpClient client = new OkHttpClient();' )
30+ . blank ( )
31+
2932 if ( source . postData . text ) {
30- RequestBody body = RequestBody . create ( JSON , json ) ;
31- code . push ( 1 , 'RequestBody body = RequestBody.create("%s", %s);' , source . postData . mimeType , JSON . stringify ( source . postData . text ) )
33+ if ( source . postData . boundary ) {
34+ code . push ( 'MediaType mediaType = MediaType.parse("%s; boundary=%s");' , source . postData . mimeType , source . postData . boundary )
35+ } else {
36+ code . push ( 'MediaType mediaType = MediaType.parse("%s");' , source . postData . mimeType )
37+ }
38+ code . push ( 'RequestBody body = RequestBody.create(mediaType, %s);' , JSON . stringify ( source . postData . text ) )
3239 }
33-
40+
3441 code . push ( 'Request request = new Request.Builder()' )
35- code . push ( '.url("%s")' , source . fullUrl )
36- if ( source . postData . text ) {
37- code . push ( 1 , '.%s(body)' , source . method )
38- } else {
39- code . push ( 1 , '.%s()' , source . method )
42+ code . push ( 1 , '.url("%s")' , source . fullUrl )
43+ if ( source . postData . text ) {
44+ code . push ( 1 , '.%s(body)' , source . method . toLowerCase ( ) )
45+ } else {
46+ code . push ( 1 , '.%s()' , source . method . toLowerCase ( ) )
4047 }
41-
48+
4249 // Add headers, including the cookies
4350 var headers = Object . keys ( source . allHeaders )
4451
@@ -50,8 +57,8 @@ module.exports = function (source, options) {
5057 }
5158
5259 code . push ( 1 , '.build();' )
53-
54- code . push ( 'Response response = client.newCall(request).execute();' )
60+ . blank ( )
61+ . push ( 'Response response = client.newCall(request).execute();' )
5562
5663 return code . join ( )
5764}
0 commit comments