How to pass Json Request body from one HttpRequest as a value in the next http request? - rest

I'm trying to capture payload (in JSON) created from an HTTP request and pass it as a value to the next API request.
Step1: Create Http Request Payload. Sample Below:
{
"fdCustomerId":"${cuid}",
"account":{
"type":"CREDIT",
"credit":{
"cardNumber":"ENC_[${Output2}]",
"nameOnCard":"John Smith",
"cardType":"${cardtype}",
"cardSubType": "${cardsubtype}",
"billingAddress":{
"type":"work",
"country":"US",
"primary":true
}
}
Step2: Capture the final Payload into a variable using post processes
var requestBody = ctx.getCurrentSampler().getArguments().getArgument(0).getValue();
vars.put("requestBody", requestBody);
log.info("###########################################Request Body are:##########" + requestBody);
Step3: Pass the RequestBody variable as a value to the next HTTP request
{
"category": "GBS_ExecMetrics_UCom",
"consumed": false,
"data": { "Test Case Id": "AB_CMS_006_CC_001_500_",
"Account Number": "0001210520779700304",
"Primary Card Number": "**${requestBody}**",
"Secondary Card Number": "0000377883144114646",
"Run Date Time": "03/26/201917:30"}
}
When I hit that to the endpoint I get below error message:
{"Error":"BadRequest: Please provide a valid Json"...
How to convert this to string or include escape characters with a function and pass the request-body?

My bad with the sample request syntax taken in step 1.Above steps worked as-is.
Corrected the syntax as below and was able to pass the json. Thanks.
{
"fdCustomerId":"${cuid}",
"account":{
"type":"CREDIT",
"credit":{
"cardNumber":"ENC_[${Output2}]",
"nameOnCard":"John Smith",
"cardType":"${cardtype}",
"cardSubType": "${cardsubtype}",
"billingAddress":{
"type":"work",
"country":"US",
"primary":true
}
}
}
}

Related

Clickatell One API Customer Call Back not returning anything

I have developed a call back REST service using the following link as reference https://docs.clickatell.com/channels/one-api/one-api-reference/#tag/One-API-Client-Callbacks/operation/sendPostOmniReplyCallback. I am using the sample payload as supplied by the Clickatell example using Postman and I can process and post the payload without problems. It also works when I deploy this as a public URL on the web. However, when pasting my URL in the Clickatell Customer Call Back I do not receive any call backs. I am obviously missing something or misinterpreting the sample code provided by Clickatell. Please note that for testing purposes I write the full payload to MySQL - works when using Postman, but not from Clickatell Call Back URL.
enter code here
{
[Route("client-callback/one-api-reply")]
[ApiController]
public class MessageController : ControllerBase
{
[HttpPost]
public async Task<dynamic> StartProcessiongAsync([FromBody] dynamic obj)
{
string myObj = JsonConvert.SerializeObject(obj);
var data = JsonConvert.DeserializeObject<Rootobject>(myObj);
var context = new DataContext();
var command = context.Database.GetDbConnection().CreateCommand();
context.Database.OpenConnection();
command = context.Database.GetDbConnection().CreateCommand();
{
command.CommandText =
"INSERT INTO testcallback(message) VALUES ('" + myObj + "');";
command.ExecuteNonQuery();
}
return Ok(obj);
}
}
}
Postman Payload Sample
{
"integrationId": "fa8090815f05d5ed015f0b5b56080cd2",
"integrationName": "My integration",
"event": {
"moText": [
{
"channel": "sms",
"messageId": "3a89680503414383af44dcd0e4e5f184",
"relatedMessageId": "d33eef0838a121f71069a4cc8d55c19e",
"relatedClientMessageId": "d33eef0838a121f71069a4cc8d55c19e",
"from": "2799900001",
"to": "2799900001",
"timestamp": 1506607698000,
"content": "Text Content of the reply message",
"charset": "UTF-8",
"sms": {
"network": "Network ID"
},

Using Request Body in Azure Data Factory

I have a working GET request in Postman where the body contains the query as seen below
The body is as follows
{
"query": {
"bool": {
"must": [
{
"term": {
"Vrvirksomhed.cvrNummer": "12345678"
}
}
]
}
}
}
Now i'm trying to get the same GET to work in Aure Data Factory but somehow it seems that the syntax needs to be different as it' doesn't use it correctly. Does it need to be wrapped somehow ?
This is because the ADF will ignore the Request body when your Request method is GET. So it can't work.
You can click '{}' button to view the code of Copy activity.
Even if your request body has content, there isn't requestBody property in source.
If you change your request method to POST, it will show.
So you can change your request method to POST to have a try.

API V1 RESTful. I got "Unknown error" when added line break to JSON. Is it OK?

I'm testing API queries and sending POST with such body:
{
"books": "12",
"available": "true",
"id": "qwe2323-2342rfws-23r2rfew"
}
I got 200 response - OK.
But when I add line break to one of the string data, e.g.:
{
"books": "12",
"available": "true",
"id": "qwe2323-2342rfws-23r
2rfew"
}
I got 500 "Unknown error".
My question: Can the server recognize this error and return a response, for example, WRONG_ID? In fact, I just added a line break character to the identifier string. In theory, the script should see the forbidden symbol without problems and return the corresponding error. Can I give such a recommendation to fix this error?
The payload is INVALID which generates an exception at the server and responded with 500 as it cannot understand the data payload.
To check the payload and show a INVALID JSON DATA or through any custom message, you can check the payload on every POST request at the server side, as in below example for PHP programming language:
private function handleRequest(Request $request, Programmer $programmer)
{
// ...
if ($data === null) {
throw new \Exception(sprintf('Invalid JSON: '.$request->getContent());
}
// ...
}
Reference:
https://knpuniversity.com/screencast/rest/error-invalid-json#handling-invalid-json

Need one help regarding fetching data value from response

Need one help regarding fetching data value from response.
Below is my response which I got after hitting URL.
{
"response": {
"Error Message": "Invalid Input missing",
"success": "false""
}
}
In this I want to read "Error Message" through POSTMAN test. For same reason I have written below code, but it is not working due to space between key.
var data = JSON.parse(responseBody);
tests ["Verify Error message"] = data.response.Error Message==="Invalid Input - Mandatory data(Company ID/source Id/SalesRep Ids/ContactPerson Ids) missing";
You're trying to use Error Message as a field with a space in it. Try:
tests ["Verify Error message"] = data.response.["Error Message"]==="Invalid Input - Mandatory data(Company ID/source Id/SalesRep Ids/ContactPerson Ids) missing";
This is not good to compare string.
In your response you must have like this,
{
"response": {
"Error Message": "Invalid Input missing",
"success": "false",
"responseCode" : 400
}
}
for more response codes, please go to this link,
http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
Then after do comparison,
var data = JSON.parse(responseBody);
if(data.reponse.responseCode == '400'){
// do stuff
}

How can I have multiple responses from a single endpoint with different parameters?

We are looking at using the API Blueprint. There are cases where we would like one request to return a proper response and another request to return an 'error' response such as a 400 bad request so that other developers can work against the mock API on apiary.io with both types of responses and handle it in their applications.
I've created a completely arbitrary example below,
## Thing [/thing/{id}]
Gets a thing but the thing id must be a prime number!
+ Parameters
+ id (string) ... ID of the thing, a prime number!
+ Model (application/json)
The thing itself.
+ Body
{
"description": "It is green"
}
### Retrieve a Single Gist [GET]
+ Response 200
[Gist][]
Now somehow I would like to add in a response for /thing/40
+ Response 400
{ "error" : "Invalid request" }
But I am not sure how I would do this with the API Blueprint. This was achievable under the 'old' style on apiary.io but we'd like to move on to the new syntax
To document multiple responses simply add it after the Response 200 like so:
## Thing [/thing/{id}]
Gets a thing but the thing id must be a prime number!
+ Parameters
+ id (string) ... ID of the thing, a prime number!
+ Model (application/json)
The thing itself.
+ Body
{
"description": "It is green"
}
### Retrieve a Single Gist [GET]
+ Response 200
[Thing][]
+ Response 400 (application/json)
{ "error" : "Invalid request" }
Note there is currently no dedicated syntax to discuss the conditions (when this response i s returned). You can discuss it anyway you like it for example:
+ Response 400 (application/json)
This response is returned when no `Thing` for given `id` exists.
+ Body
If you are using Apiary mock, keep in mind that the first response listed is returned by default, unless you say otherwise using the prefer HTTP header.
You can use the templates and specify a specific use case after a generic response for your resource, see example:
Reservation [/reservation/{reservation_key}]
A Reservation object has the following attributes:
room_number
reserved_at - An ISO8601 date when the question was published.
booker_details - A Booker Object.
Parameters
reservation_key (required, text, 1) ... Reservation Key ash
View a Reservation Detail [GET]
Response 200 (application/json)
{
"room_number": 55,
"reserved_at": "2014-11-11T08:40:51.620Z",
"booker_details":
{
"name": "Jon",
"last_name": "Doe",
}
}
Reservation [/reservation/notarealreservation123]
Using a non existing reservation (pls use notarealreservation123 in the fake) returns not found