Collection variable access in a request body postman - collections

I have set collection variable in test script to my previous request. It has been set perfectly. Afterwards when I am trying to access it in body of my next request I am seeing the values on hoover, but it doesn't responds successfully.
Here is my script from 1st request:
console.log(jsonData.product.id);
pm.collectionVariables.set("option_id1",jsonData.product.variations[0].combination[0].option_id);
pm.collectionVariables.set("option_id2",jsonData.product.variations[0].combination[1].option_id);
pm.collectionVariables.set("option_v_id1",jsonData.product.variations[0].combination[0].option_value_id);
pm.collectionVariables.set("option_v_id2",jsonData.product.variations[0].combination[1].option_value_id);
pm.collectionVariables.set("sku_channel_id1",jsonData.product.variations[0].channel_data[0].sku_channel_id);
pm.collectionVariables.set("sku_channel_id2",jsonData.product.variations[0].channel_data[1].sku_channel_id);

Related

How can a server handle HTTP requests in the order in which they were sent?

I'm building a simple CRUD application where there is a client, a server, and a database.
The client can send two types of requests to the server:
POST requests that update a row in the database.
GET requests that retrieve a row in the database.
One requirement of my application is that the requests have to be handled in the order in which they were sent from the client. For example, if the user makes a POST request to update row 1 in the database, then they make a GET request to retrieve row 1 in the database, the response to GET request must reflect the changes made by the previous POST request. One issue is that there is no guarantee that the server will receive the requests in the same order that the requests went sent. Therefore, in the example above, it is possible that the server might receive the GET request first, then the POST request later, which makes the response to the GET request not able to reflect the changes made by the previous POST request.
Another requirement of the application is that the client should not have to wait for the server's response before sending another request (this constraint is to allow for faster runtime). So in the example above, one possible solution is let the client wait for the server response to the POST request, and then it can send the GET request to retrieve the updated row. This solution is not desirable under this constraint.
My current solution is as follows:
Maintain a variable on the client that keeps track of the count of all the requests a user has sent so far. And then append this variable to the request body once the request is sent. So if the user makes a POST request first, the POST request's body will contain count=1, e.g. And then if they make a GET request, the GET request's body will contain count=2. This variable can be maintained using localStorage on the client, so it guarantees that the count variable accurately reflects the order in which the request was made.
On the server side, I create a new thread every time a new request is received. Let's say that this request has count=n. This thread is locked until the request that contains count=n-1 has been completed (by another thread). This ensures that the server also completes the requests in the order maintained by the count variable, which was the order in which the request was made in the client.
However, the problem with my current solution is that once the user clears their browsing data, localStorage will also be cleared. This results in the count getting reset to 0, which makes subsequent requests be placed in a weird order.
Is there any solution to this problem?

Change the response status code for ngx.exec in Openresty

I am returning a stored custom HTML file for a particular request using ngx.exec function. I want to set the right HTTP response code. I am not able to figure out how to do that. Even if I set ngx.status before ngx.exec the response code returned is always 200 to the client.
Is there a way to set the response code?

Post request handling with lua script in nginx

I use lua script to generate data from the parameters and bodies, and then send the data to the other server.
When i handle a GET request, nginx returns a normal response.
However, a 404 not found error occurs when handling POST requests.
However, internal operations were normal and sent the data to the other server.
Only the request method has changed.
If i explicitly pass the value to ngx.say or ngx.exit, i get 200 response normally.
Why? Is it necessary to explicitly return a response code when using a post request with a lua script?
In addition, I am using empty_gif.
I have searched for the above problem.
empty_gif can only be used to respond to GET and HEAD request methods
so I will use 204 response code

JMeter http request DELETE with body

I have proprietary http based API to test from JMeter. Unfortunately some of the endpoints of the API are expecting http DELETE method with a request body (I know its questionable API design to use DELETE with request body, but I have no ability to change that API and need to test it).
How can I test it from JMeter? It seems that standard HttpRequest sampler silently ignores my body payload without any warnings. (When I try it in POSTMAN its sending a request body for DELETE method)
I did find an old JMeter plugin called HTTP Raw Request that somewhat helps but still doesn't auto-calculate "Content-Length:" http header for my body payload...so I have to do it manually for every test case - which is a pain for dynamically generated data payloads.
So my question still remains: How can I test HTTP DELETE with request body from JMeter?
Here is the screenshot:
NOTE1: Starting from jMeter ver. 3.1 (see bugzilla #60358) it was fixed for Http GET request to be able to send body in the request...but DELETE was not added.
NOTE2: See bugzilla #61443 for the DELETE request with body.
NOTE3: I'm using client implementation called "Java".
As per reference docs:
http://jmeter.apache.org/usermanual/component_reference.html#HTTP_Request
There are 2 implementations for http request sampler. The non default one called "Java" has this issue with not passing DELETE request body.
Luckily the default implementation called "HttpClient4" that correctly passes request payload for DELETE operation as per JMeter data entry screen.
To change implementations for http request sampler you have to go to "advanced" tab of the HTTP Request Sampler and select client implementation "HttpClient4" instead of "Java". Here is the screenshot:
After that when executed it correctly sends request payload for DELETE operation. Here is the screenshot:

Http status code return

What HTTP status code should be sent to user if his post request is correct but there is nothing has been updated in database as user is sending the same value for every field which already been there in database?
A 200 status would definitely be perfectly appropriate in this case.
What you are describing is usually something that an application on top of the HTTP-based API would handle/add as context.
One lesser-known status code which could be used in such cases, however, is 204.
"The 204 (No Content) status code indicates that the server has
successfully fulfilled the request and that there is no additional
content to return in the response payload body"
In other words, depending on your application's setup, you could use a 204 (with no response body) to indicate that the PUT/update request itself was successful. but that nothing was modified.
See here for further reading on 204: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-p2-semantics-19#section-7.2.5
You will use 204 in this case.
The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.
If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.
The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

Resources