POST in JMeter gives "Unrecognized token 'json'" - json

I can not figure out why I get this error:
"Failed to parse request body as JSON resource. Error was: Failed to parse JSON encoded FHIR content: Unrecognized token 'json': was expecting ('true', 'false' or 'null')\n at [Source: UNKNOWN; line: 3, column: 29]"
FHIR is the standard used. I also tested with a valid JSON that worked with Postman, so I don't think the actual JSON is the issue.
I'm not sure if I'm correct, but it seems JMeter adds 'json' from somewhere as the error states the token 'json' is unexpected. This is the Request > Request body tab in View Results Tree.
This is just a test JSON, but I got the same response with a JSON body that is working in Postman (and I formatted correctly to be sure). I have the Content-Type header specified. I simply don't understand where the token 'json' would come from as my json itself doesn't contain the token. Does anybody know if JMeter adds something to the request?

You're sending incorrect payload, it should look like:
{
"test" : "X"
}
and you are sending
{
"test" : "X"
}json
^^^^ this guy is causing the issue
JMeter doesn't add anything to request, you need to double check your configuration, i.e. JMeter jmx scripts are "normal" XML files so you can use your favorite text editor to look up this json
If you're able to send a valid request using Postman you should be able to record it using JMeter's HTTP(S) Test Script Recorder, just configure Postman to use JMeter as the proxy and run your request/collection - JMeter will capture the requests and generate relevant HTTP Request samplers which can be replayed successfully.
More information: How to Convert Your Postman API Tests to JMeter for Scaling

It happened to be that if you add a default parameter in an HTTP Request Defaults (in my case _format=json), it would add it to the body of the POST.
I fixed this by adding a BeanShell PreProcessor with the code:
if(sampler.getMethod().equalsIgnoreCase("get")){ sampler.addArgument("_format", "json"); }

Related

API endpoint returns text before and after the JSON content, causing parsing error

I'm using postman to test calling a rest service endpoint.
I'm trying to parse the JSON return content but it throws an error because the response body has more than just JSON.
This is how I parse it in my postman test script:
var jsonData = JSON.parse(responseBody);
Here is the response body:
--13398550-b6ea-4731-a8ee-4b2ad24c3cfe
Content-Type: application/json; charset=utf-8
//this is the actual content I want to parse --->
{"id":"123456","value":"the_value"}
--13398550-b6ea-4731-a8ee-4b2ad24c3cfe--
When I try to parse it, I get the following error (in postman)
There was an error in evaluating the test script: SyntaxError:
Unexpected number in JSON at position 3
Obviously because the content being parsed is not just JSON
Is this something special that the api is doing? Or am I just parsing it incorrectly?
NOTE: I'm not including details of the rest service function. If the cause of this issue is something that is being done by the service itself, then that is enough of an answer for me to perhaps ask another question or do some further investigation. The purpose of this question is to ask whether this is something special being done in HTTP, or if it's the service.
Edit:
I managed to see the server side code and it is indeed manually building the response with boundaries identified by a GUID. I'll have to manually parse the response
The server is not emitting straight up application/json, it's packed in a multipart mime envelope.
Whether or not it's doing that correctly depends on the response headers. If you didn't expect a multipart response, but a simple JSON response, then I'd say yes: it's something you need to fix server-side.

Json request validation in Jmeter

As my json request contains all mandatory parameters in request body, I want to validate all parameters using Jmeter.
Do let me know if it is possible to validate all request parameters presents in json body using jmeter or jmeter plugins
Normally people are interested in validating responses, not the requests therefore I am not aware of any suitable Test Elements either bundled or available via Plugins. For responses validation you have JSON Path Assertion
If for some reason you need to validate the request and fail the sampler if the validation fails you can use JSR223 Post Processor. Example code:
def request = sampler.getArguments().getArgument(0).getValue()
def json = new groovy.json.JsonSlurper().parseText(request)
// do any checks you need, for example
if (!json.keySet().contains("somekey")) {
log.info("Key \"somekey\" was not found in the request")
prev.setSuccessful(false)
}
References:
JsonSlurper
Parsing and producing JSON
Groovy Is the New Black

Not able to access a string value in aws lambda function from AWS API request

I created a sample AWS lambda function and integrated this function with AWS API.
I have written a post method in API and selected application/JSON whose request integration mapping is as below.
{
'songTitle':"$input.params('songTitle')"
}
songTitle is being sent as parameter (application/JSON) with request to API.
However I am receiving $input.params() as empty.
As I am new to this I have no idea what is the correct way.
Please guide to access these params
I'm not sure if this is your issue, but single quotes are not the same as double quotes in JSON. Anything in the mapping template that isn't escaped with the $ character is written as-is in the actual payload.
Should be:
{
"songTitle":"$input.params('songTitle')"
}
Edit: If that doesn't work, please also test by sending the Content-Type header in the request as application/json. If you don't send Content-Type at all, it should default to application/json mapping template.
In case of reading input parameters from request header part (for request integration mapping)
syntax:
{
'songTitle':"$input.params('songTitle')"
}
If we want to read parameters from request body part we should write as
{
'songTitle':"$input.path('$.songTitle')"`enter code here`
}

How to pass parameter and json both in http request request body in jmeter?

I need to pass those parameters and JSON in a request body of HTTP request in Jmeter.
UserId=47&Token=c41240ca195c47b7821465d328d5af16&ClientId=1&meeting={
"InternetConnectionType":"WIFI",
"DeviceID":"3d6f9c346da00a32",
"NetworkCarrierName":"VODAFONEIN",
"BatteryValue":"22",
"AppVersion":"1.1.3",
"OSVersion":"4.4.2",
"InLongitude":"77.0384036",
"Remarks":"hii hcl",
"LastPhoneRestart":"Wed Jun 15 14:32:14 GMT+05:30 2016",
"OutDateTime":"20-06-2016 11:54:09",
"OutLongitude":"77.0384036",
"IsApproved":"0",
"SimNo":"",
"OutLatitude":"28.4181932",
"AttendanceType":"G",
"InDateTime":"20-06-2016 11:54:08",
"GeofenceAssignedId":"10114",
"LastWifiNetworkName":"YomaTech",
"InLatitude":"28.4181932",
"IMEINo":"359453066495811",
"DeviceDateTime":"20-06-2016 11:54:08",
"GeofenceId":"1"
}
Something like:
Out of interest and just in case it helps to avoid similar questions in the future: why don't you just record your test scenario with JMeter's Proxy Server or JMeter Chrome Extension?
I have tried below solution in JMeter 4, and it worked for me.
Follow below steps:
1) Add "HTTP Header Manager"
2) Add required headers and parameters in key-value format
3) Add "HTTP Request
4) Provide server name, port, method=POST and path
5) Select Body Data tab
6) Enter json body
Hope it helps.
I tried solve similar problem in current version of Apache JMeter 5.1.
I set up JSON body for HTTP Request element (bookmark Body Data).
Added parameter into HTTP Default element (bookmark Parameters).
In this case parameter were added after JSON body and test fails for bad JSON format.
My solution was added parameter into path in HTTP request. But default behavior seems little buggy for me.

cannot get document id using box view api

In my app, after get "https://dl.boxcloud.com/*" url and send it to the View API, I receive this error:
{
message: "JSON parse error - No JSON object could be decoded"
type: "error"
request_id: "3ef12abcaf7a4c5abab5fb0d3959255e"
}
you can use this chrome extension to recreate this error
https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
I tried with the other rest clients and it work correctly. Except in my app and the extension above.
I tried with the other rest clients and it work correctly. Except in my app and the extension above.
If this is the case, it's likely that the client you're using isn't actually sending properly formatted JSON. One way to debug this would be to output the raw HTTP request the client is sending and ensuring that it's actually sending the JSON properly.