Send model id in Put Body not in url - rest

As we know the format of PUT method in Restful should be something like this:
/api/user/{id}
and the PUT body
But is it OK if I just call /api/user and I send the {id} inside the PUT body like this: {"id": "1", "name": "myname"};

Related

GET with body (json format), what to do with apostrophe?

I am new to REST. I have a client who tries call an api I have running. Problem is that they use some type of builder to create the body.
In my endpoint, I expect name field in the body like this:
{
"name": [
{ "tag": "1", "value": "O'Bryan" },
...
But client sends something like this:
{
'name': [
{'tag': '1 ', 'value': 'O'Bryan'}
It works except when name has apostrophe. I am wondering if this body will still valid. If not, what can they do to fix it without using double quote(")?
This body will not be valid, you would need to escape the single quote like so \'

How to get a xml attribute variable (type double) using xPath in response using Wiremock

Request is an xml like this:
<Request>
<Account>373953192351004</Account>
<Amount>98.21</Amount>
</Request>
Response is a json and should have amount mapped from request but data type being a decimal number (not String) e.g.
{
"response": {
"status": "SUCCESS",
"amount": 98.21
}
}
i was able to do it with xPath using json mapping like this:
"amount": "{{xPath request.body '/Request/Amount/text()'}}"
but above xpath makes amount a string with double quotes in the response, like:
"amount": "98.21"
How do i make it without quotes like:
"amount": 98.21
note: I can't remove the quotes in "{{xPath... as it wouldn't be a valid json anymore.
You need to remove the double quotes from outside the {{ xPath ... }}. So:
"amount": {{xPath request.body '/Request/Amount/text()'}}

Passing multiple json as a payload for a request in Gatling

sample json payload:
'{
"Stub1": "XXXXX",
"Stub2": "XXXXX-3047-4ed3-b73b-83fbcc0c2aa9",
"Code": "CodeX",
"people": [
{
"ID": "XXXXX-6425-EA11-A94A-A08CFDCA6C02"
"customer": {
"Id": 173,
"Account": 275,
"AFile": "tel"
},
"products": [
{
"product": 1,
"type": "A",
"stub1": "XXXXX-42E1-4A13-8190-20C2DE39C0A5",
"Stub2": "XXXXX-FC4F-41AB-92E7-A408E7F4C632",
"stub3": "XXXXX-A2B4-4ADF-96C5-8F3CDCF5821D",
"Stub4": "XXXXX-1948-4B3C-987F-B5EC4D6C2824"
},
{
"product": 2,
"type": "B",
"stub1": "XXXXX-42E1-4A13-8190-20C2DE39C0A5",
"Stub2": "XXXXX-FC4F-41AB-92E7-A408E7F4C632",
"stub3": "XXXXX-A2B4-4ADF-96C5-8F3CDCF5821D",
"Stub4": "XXXXX-1948-4B3C-987F-B5EC4D6C2824"
}
]
}
]
}'
I am working on a POST call. Is there any way to feed multiple json files as a payload in Gatling. I am using body(RawFileBody("file.json")) as json here.
This works fine for a single json file. I want to check response for multiple json files. Is there any way we can parametrize this and get response against multiple json files.
As far as I can see, there's a couple of ways you could do this.
Use a JSON feeder (https://gatling.io/docs/current/session/feeder#json-feeders). This would need your multiple JSON files to be in a single file, with the root element being a JSON array. Essentially you'd put the JSON objects you have inside an array inside a single JSON file
Create a Scala Iterator and have the names of the JSON files you're going to use in it. e.g:
val fileNames = Iterator("file1.json", "file2.json)
// and later, in your scenario
body(RawFileBody(fileNames.next())
Note that this method cannot be used across users, as the iterator will initialize separately for each user. You'd have to use repeat or something similar to send multiple files as a single user.
You could do something similar by maintaining the file names as a list inside Gatling's session variable, but this session would still not be shared between different users you inject into your scenario.

JSON server - Is it possible to update an object id?

Here is my db.json :
{
"users": [
{
"id": "1"
"name": "John"
}
]
}
I'd like to be able to update the user id by sending a PUT request on the existing user. But the following does not work:
Request URL :
PUT /users/1
with body:
{
"id": "2"
"name": "John"
}
Is there a way to update an object id?
If you are using PUT request means ,the request URL should be like this "PUT/users/1" .
Refer below mentioned image.
I'm using postman to send put request
This does not seem to be possible, as said in documentation:
Id values are not mutable. Any id value in the body of your PUT or
PATCH request will be ignored. Only a value set in a POST request will
be respected, but only if not already taken.

How to construct a REST API take Json payload as the input?

I have a HTTP REST api wanting to wrap a underlying datasource(can be RDBMS like mysql or something else, like HBase). I want to construct an REST API and wrap the underlying implementation, so my API might look like:
http://${APIServer}/${TableName}?attrs=A,B,C&${json_payload}
The payload looks like:
{
"like": {
"name": "kev"
},
"equal": {
"id": "2",
"sex": "male"
}
}
To achieve something like:
select A,B,C from TableName where name=shengjie and address like %Ireland%
I want to wrap the WHERE conditions into the json_paylaod, is there any best practice for this?