Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.

Commit bd20914

Browse files
committed
PHP JSON body code generation
1 parent 4c0c5ea commit bd20914

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/targets/php/curl.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict'
1212

1313
var util = require('util')
14+
var helpers = require('./helpers')
1415
var CodeBuilder = require('../../helpers/code-builder')
1516

1617
module.exports = function (source, options) {
@@ -67,9 +68,13 @@ module.exports = function (source, options) {
6768
name: 'CURLOPT_CUSTOMREQUEST',
6869
value: source.method
6970
}, {
70-
escape: true,
71+
escape: !source.postData.jsonObj,
7172
name: 'CURLOPT_POSTFIELDS',
72-
value: source.postData ? source.postData.text : undefined
73+
value: source.postData ? (
74+
source.postData.jsonObj ?
75+
'json_encode(' + helpers.convert(source.postData.jsonObj, opts.indent) + ')' :
76+
source.postData.text
77+
) : undefined
7378
}]
7479

7580
code.push('curl_setopt_array($curl, array(')

src/targets/php/http1.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ module.exports = function (source, options) {
6666
.blank()
6767
break
6868

69+
case 'application/json':
70+
code.push('$request->setContentType(%s);', helpers.convert(source.postData.mimeType))
71+
.push('$request->setBody(json_encode(%s));', helpers.convert(source.postData.paramsObj, opts.indent))
72+
.blank()
73+
break
74+
6975
default:
7076
if (source.postData.text) {
7177
code.push('$request->setBody(%s);', helpers.convert(source.postData.text))

src/targets/php/http2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ module.exports = function (source, options) {
7575
hasBody = true
7676
break
7777

78+
case 'application/json':
79+
code.push('$body = new http\\Message\\Body;')
80+
.push('$body->append(json_encode(%s));', helpers.convert(source.postData.jsonObj, opts.indent))
81+
break;
7882
default:
7983
if (source.postData.text) {
8084
code.push('$body = new http\\Message\\Body;')

0 commit comments

Comments
 (0)