apimocker icon indicating copy to clipboard operation
apimocker copied to clipboard

Json coming in Request Parameter

Open mmalik786 opened this issue 10 years ago • 7 comments

Is the JSON coming in Request Parameter supported ? like :

responseJSON= { "customerId" : "12"; "city" : "Cincinnati"

}

can you use a switch statement on the request parameter : responseJSON and then parse it for customerId or city or both ?

Thanks

mmalik786 avatar Feb 05 '15 20:02 mmalik786

Yes, you can use JSON Path to evaluate your JSON request, and return different responses: https://github.com/gstroup/apimocker#jsonpath-support Let me know how that works for you. Thanks.

gstroup avatar Feb 06 '15 17:02 gstroup

But my JSON is coming in of the request parameters and I believe that is not supported. since in the switch you specify $ not the request param.

can you help me with that ?

mmalik786 avatar Feb 06 '15 18:02 mmalik786

for example:

"switch": "$..ItemId[(@.length-1)]", "responses": { "post": {"httpStatus": 200, "mockFile": "aceinsleeve.json"} }, "switchResponses": { "$..ItemId[(@.length-1)]4": {"httpStatus": 500, "mockFile": "ItemId4.aceinsleeve.json"} } }, this is assuming the json is coming in a request body

but if you see my previous example:

responseJSON= { "customerId" : "12"; "city" : "Cincinnati"

}

my json is coming in the request param - responseJSON responseJSON needs to be in the switch correct?

Thanks

mmalik786 avatar Feb 06 '15 18:02 mmalik786

Ok, I see what you mean now. JSON path evaluation for switching on complex requests is only supported for JSON Post requests right now. The POST method is the standard way for sending JSON data to an API. Sending JSON in a GET request is not a really good idea. Is it possible to change your API so you can POST the JSON?? If not, I could take a look to see if what it would take to add JSON path support for GET requests.

Thanks.

-Greg

On Feb 6, 2015, at 10:41 AM, mmalik786 [email protected] wrote:

for example:

"switch": "$..ItemId[(@.length-1)]", "responses": { "post": {"httpStatus": 200, "mockFile": "aceinsleeve.json"} }, "switchResponses": { "$..ItemId[(@.length-1)]4": {"httpStatus": 500, "mockFile": "ItemId4.aceinsleeve.json"} } }, this is assuming the json is coming in a request body

but if you see my previous example:

responseJSON= { "customerId" : "12"; "city" : "Cincinnati"

}

my json is coming in the request param - responseJSON responseJSON needs to be in the switch correct?

Thanks

— Reply to this email directly or view it on GitHub.

gstroup avatar Feb 07 '15 20:02 gstroup

We are doing a post but using the parameter instead of the JSON in the body itself.

Cannot change the API!! If you could see how this can be supported. Also I don't mind doing a code change myself if you can guide me where the code changes are ..

Can you let me know ? And also provide me an example how will this work ?

Thanks

Best, Muneer Malik

On Feb 7, 2015, at 2:09 PM, gstroup [email protected] wrote:

Ok, I see what you mean now. JSON path evaluation for switching on complex requests is only supported for JSON Post requests right now. The POST method is the standard way for sending JSON data to an API. Sending JSON in a GET request is not a really good idea. Is it possible to change your API so you can POST the JSON?? If not, I could take a look to see if what it would take to add JSON path support for GET requests.

Thanks.

-Greg

On Feb 6, 2015, at 10:41 AM, mmalik786 [email protected] wrote:

for example:

"switch": "$..ItemId[(@.length-1)]", "responses": { "post": {"httpStatus": 200, "mockFile": "aceinsleeve.json"} }, "switchResponses": { "$..ItemId[(@.length-1)]4": {"httpStatus": 500, "mockFile": "ItemId4.aceinsleeve.json"} } }, this is assuming the json is coming in a request body

but if you see my previous example:

responseJSON= { "customerId" : "12"; "city" : "Cincinnati"

}

my json is coming in the request param - responseJSON responseJSON needs to be in the switch correct?

Thanks

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

mmalik786 avatar Feb 07 '15 20:02 mmalik786

Hi Muneer,

That's a strange API you're calling! : )

Anyway, take a look at line 219 in the apimocker.js code. That's where the JSON path evaluation happens, on the request body. We'll have to add another few lines of code there, to evaluate a query param. I think we should add another possible setting to the config.json, something like "jsonRequestParam". Then in the code, we check if that is set, and if so, run the JSON path evaluation on that.

Sorry for my slow response, I'm pretty busy with other "real" work. If you are able to make the change, and send a pull request, that would be great! If not, I'll try to make that change later this week.

Thanks.

-Greg

gstroup avatar Feb 09 '15 17:02 gstroup

Thanks Let me look at the code and see if I can make the change. when you can, whenever you get to it next week .. let me know Thanks

mmalik786 avatar Feb 09 '15 17:02 mmalik786