ContentType.URLENC with sending API REQUEST - java

I started API testing recently and facing different content-types each time, and i wan to know specific in what case we use ContentType.URLENC, I kinda sure we use it with x-www-form-urlencoded, but still confusing and we use it with usually POST request ?

Firstly, when you want to send infomation to server, you need to tell the server how you structure your message in BODY part. Below is some basic ContentType:
Using key-value with&: you can choose x-www-form-urlencoded.Since there are some special characters in your message, then these need to be encoded. That's why urlencoded.
Eg: name=abc&age=20
Json type: choose application/json .
Eg:
{
"name": "abc",
"age": 20
}
Want to send a file: choose form-data
Secondly, BODY part usually goes with POST and PUT request.

Related

JMeter variables from one post http request to another

I've got a JMeter issue... The problem is: i'm sending a http request by POST method to a website and then the page returns me 100 responses, for example one of the answers:
Answer:
{
"Category": "XX",
"NumberX": 5100051,
"Class": "CLASS_1",
"NumberY": 5100136,
"arrivalDate": "2022-06-22T04:36:00",
"departureDate": "2022-06-21T23:58:00"
},
and then i have to save some of the values with a JSON extractor to variables.
Then I have second one http request by POST method, where in the body i have to use these variables. And my problem is that i don't how to count these variables in the body... With one data it's working, for more i don't know how to iterate it... Body in the second request:
{
"vehicleCategory": "${Category}",
"NumberOfTasks": "${NumberX}",
"departureDate": "${departureDate}",
"arrivalDate": "${arrivalDate_}"
}
I want to see query responses to each of the previous 100 values...
I hope i I clearly described the problem... Is there anybody who can help me to do it?
If you give "Match No" as -1 in the JSON Extractor:
it will generate the following JMeter Variables:
Category_1=first match
Category_2=second match
....
Category_matchNr=100 (or whatever is the number of matches)
So you will be able to iterate these values by:
Adding a Loop Controller with "Loop Count" set to ${Category_matchNr}
In the HTTP Request body reference the variables as:
{
"vehicleCategory": "${__V(Category_${__intSum(${__jm__Loop Controller__idx},1,)},)}",
"NumberOfTasks": "${__V(NumberX_${__intSum(${__jm__Loop Controller__idx},1,)},)}",
"departureDate": "${__V(departureDate_${__intSum(${__jm__Loop Controller__idx},1,)},)}",
"arrivalDate": "${__V(arrivalDate__${__intSum(${__jm__Loop Controller__idx},1,)},)}"
}
More information: Here’s What to Do to Combine Multiple JMeter Variables

Unable to retrieve data from Sabre PriceQuoteServicesRQ 4.10 SOAP API: XML request schema validation failed: PriceQuoteInfo element is not complete

I have a problem when trying to retrieve data from Manage Price Quote Details (PriceQuoteServicesRQ) 4.10 Sabre SOAP API.
I generated Java classes using the WSDL from Sabre website (https://developer.sabre.com/docs/soap_apis/air/fulfill/manage_price_quote_details/resources).
I am constructing my request object in a following way:
ReservationTypeShort reservation = new ReservationTypeShort();
reservation.setValue("YEZUYS");
PriceQuoteInfoSearchParameters info = new PriceQuoteInfoSearchParameters();
info.setReservation(reservation);
PriceQuoteSearchParameters searchParameters = new PriceQuoteSearchParameters();
searchParameters.getPriceQuoteInfo().add(info);
searchParameters.setResultType(StringResultType.S);
GetPriceQuoteRQ req = new GetPriceQuoteRQ();
req.setSearchParameters(searchParameters);
req.setVersion("4.1.0");
I pretty-printed the object and this is what I got:
"priceQuoteInfo" : [ {
"reservation" : {
"value" : "YEZUYS",
"createDate" : null
},
"status" : [ ],
"type" : null,
"priceQuote" : [ ],
"travelItinerary" : null
} ],
So according to their documentation:
I am supplying all fields that are necessary, however it still doesn't work for me.
Did anybody else had the same problem? What am I missing/what am I doing wrong?
This is the error message I am getting:
XML request schema validation failed: PriceQuoteInfo element is not complete. One of the following fields: Status, Type, PriceQuote, TravelItinerary should be used. Please amend your request and try again.
What I have tried so far?
I asked Sabre Support for help, but they responded with a message that basically says "it works on my end".
I intercepted the XML body:
<ns5:GetPriceQuoteRQ version="4.1.0">
<ns5:SearchParameters resultType="S">
<ns5:PriceQuoteInfo>
<ns5:Reservation>YEZUYS</ns5:Reservation>
</ns5:PriceQuoteInfo>
</ns5:SearchParameters>
</ns5:GetPriceQuoteRQ>
I was missing an empty element <PriceQuote/> in my request.
It can be added by doing:
PriceQuoteInfoSearchParameters info = new PriceQuoteInfoSearchParameters();
info.setReservation(reservation);
info.getPriceQuote().add(new PriceQuoteSearch());
So according to their documentation I am supplying all fields that are necessary [...]
By documentation do you mean the WSDL or some human readable documentation (like PDF, DOCX, web pages, etc)? According to the error message you get, your SOAP request isn't valid. Sabre Support responding with "it works on my end" is another way of saying that you are not doing something correctly on your end. You need to troubleshoot your request.
From what I see, the error message is saying Status, Type, PriceQuote, and TravelItinerary but you are sending status, type, priceQuote, and travelItinerary. XML is case sensitive, and it's possible the service field names are too, so this might be the first thing to check.
Then, two of your fields (type and travelItinerary) are null. Also, priceQuote is empty. Is that OK? This is the next thing to check.
The object you pretty-printed shows a JSON format. Is this actually the format you are sending on the wire to the service? SOAP wants XML, not JSON. You also mention you generated the code from the WSDL. Using what framework or library? Does the generated code send XML?
Like I said, you need to troubleshoot the call:
download SoapUI
feed the WSDL file to SoapUI so that it can generate sample requests for you
fill in those request with real data and make calls to the web service until you get back a successful and expected response
using the same parameters from 3) in your code, perform the same call using your code
use SoapUI's monitoring tools to intercept the request at 4) and inspect the SOAP message you are sending
check the request you are making with your code against the successful request you got by using SoapUI directly
correct any differences until your request made by code is like the one send from SoapUI and it returns a successful and expected response.

How to get a URL in response of a POST request that navigates to GET request in Spring Boot?

I'm working on a project in which I'm creating (POST) a book. Once the book is created, a unique bookId is generated and the response looks something like this:
{
"bookId":"123pqr",
"author":"Abc",
"title": "Book1"
}
I have one GET request which basically fetches the book details using bookId - http://localhost:port/{bookId}.
I'm trying to get this above URL whenever I create a book so that my response should look like this:
{
"bookId":"123pqr",
"author":"Abc",
"title": "Book1"
"url": "http://localhost:port/{bookId}"
}
So that if a user clicks on the URL, the user should be navigated to a GET request http://localhost:port/{bookId}. I think I can just hardcode a string "http://localhost:port/" and then append bookId to it and provide the same in response. However, I'm not sure how to set the request type as GET when the URL is clicked. Also, is there a better way to avoid harcoding? Could someone please help? Thanks in advance!
You do not need to specify anything to consider a URL as a GET.
This is natively part of every browser.
However, you could think a bit further and use HATEOAS which specifies the kind of relationship for a specific href
Your response would look like the following where self is standardized to be a GET request since it's the retrieval of the resource
{
"bookId":"123pqr",
"author":"Abc",
"title": "Book1"
"_links": {
"self": {
"href": "http://localhost:port/{bookId}"
}
}
}
More information can be found here
Spring HATEOAS which BTW can also help with building the URI and thus no need to hardcode localhost and port making your app dynamic enough

Comparing two JSON to make sure Key of first json is present in other one

I am writing here to get some clue regarding the problem I'm facing.
I am actually writing a service that will send text messages using some third party API. I've lots of message templates (static text and placeholders for dynamic content) that needs to be sent out and this service will be called from multiple modules with-in my application.
What I've planned is to store message templates in database and once any module require to send text message it will call my messaging service and pass data require to fill up the placeholders for a particular template.
I am saving my templates in database as follows:
{
"data": {
"message": "[GREETING], this is the test message sent on [DATE].",
"constant1": "",
"constant2": ""
},
"metadata": {
"data": {
"message": {
"[GREETING]": "required",
"[DATE]": "required"
},
"constant1": "required",
"constant2": "required",
"constant3": "optional"
}
}
}
data is the actual object I want eventually to pass messaging api and metadata is basically the constants and variables that are required/optional to complete data object.
and the data I'm receiving from other modules that are calling my service is:
{
"[GREETING]": "Dear John Doe",
"[DATE]": "28 February",
"data": {
"constant1": 982042,
"constant2": [64238, 64239]
}
}
So the issue is, I've to verify that all the required constant that are set required in templates metadata should be present the the json I'm receiving while my service is being called. I'm bit confused here of how I can compare the keys of data with template to make sure all the required data of template is sent by the module that has just called my service.
Can someone give me a clue if it's even possible in java ?
p.s: I just want some guidance or clue.
Thanks
Update: if someone have better and neat suggestion in achieving what I want to achieve in above mentioned scenario, please share I would really appreciate that.
Update2: Clarification of what I actually want to achieve: I've a JSON template in which I have a metadata field that tells me which parameters are required to fill up this specific template. So upon receiving call to my service, I've to first check that required parameters (set in metadata of templates) should be there in API request body. Hope that clears my requirements now.

Post JSON Data From Javascript To Java

I'm in need of some desperate help. I've been at this for 4 hours, and I'm getting pretty worn out. :/ Here's my situation:
I have a Javascript application that is making a POST request (using jQuery $.post) to an external site. On the external site I have Apache Camel running with Jetty to expose it to the web. The web services I wrote in Camel expect JSON data for all of the requests. For instance, one request needs an id, so I send it {"id": 10}.
Here's my issue: it doesn't work from Javascript. I have a few different tools that will send post requests for me (like the Poster extension for browsers). If I use Poster and set the body to {"id": 10}, it works just fine. I get that exact string in the service.
But, if I post from Javascript, I get something different. Posting the JSON object will give me the string "id=10" on my service side. (It's OK for this scenario, but I will need actual JSON objects eventually.) If I stringify the JSON object, I get the JSON string, only all of the characters are escaped. (Ex. "%7Bid%33...").
I swear I've tried every method possible for posting the data, but I either get the weird already parsed JSON, or the escaped string (or nothing at all). Is there some way I can have Javascript NOT parse the JSON object and just send it (like my posting tool does)? If not, is there a safe, efficient way to un-escape the JSON string that I get?
I really appreciate any help.
I feel like we need a little bit more information, but take a look at this javascript plugin. It may be your solution: https://github.com/flowersinthesand/jquery-stringifyJSON
Try using jQuery.ajax and setting processData to false (defaults to true):
$.ajax({
url: '/where/to/post',
type: 'POST',
data: {"id": 10},
processData: false
});
Usually, jQuery converts anything in data to query string format like id=10. The processData flag tells jQuery to interpret it literally as a json hashmap.
Posting the JSON object will give me the string "id=10" on my service side.
Javascript does not do your this conversion, so your server does it.
It is likely that your server reacts differently based on the content-type of your POST e.g. application/json vs text/plain or text/html, a common feature of REST based services.
The answers here gave me a few hints, but ultimately, it was a lot of tweaking before it would work correctly. I had to do 3 things:
Add processData: false.
Turn the JSON object into a JSON string. The request wouldn't fire if I left it as an object (even if I changed contentType to application/json).
Change the contentType to text/plain. This sent it as a raw string.
And that's what did the trick. I now get the JSON string I want on the server side.

Categories