How to pass HashSet to server to test API from postman? - rest

I created an API which I want to test using postman. My api is accepting many parameters and one parameter is HAshSet. I dont know how to pass HashSet parameter using postman. Please help me. Thanks in advance
Here is my code:
#PutMapping
#ApiOperation(value = "collectMultiInvoices", nickname = "collectMultiInvoices")
public BaseResponse collectAmountMultipleInvoices(#RequestParam(value = "invoice_id") HashSet<Integer> invoiceIds,
#RequestParam("date") String _date,
#RequestParam(value = "cash", required = false) Float cashAmount,
#RequestParam(value = "chequeAmount", required = false) Float chequeAmount,
#RequestParam(value = "chequeNumber", required = false) String chequeNumber,
#RequestParam(value = "chequeDate", required = false) String _chequeDate,
#RequestParam(value = "chequeImage", required = false) MultipartFile chequeImage,
#RequestParam(value = "chequeBankName", required = false) String chequeBankName,
#RequestParam(value = "chequeBankBranch", required = false) String chequeBankBranch,
#RequestParam(value = "otherPaymentAmount", required = false) Float otherPaymentAmount,
#RequestParam(value = "otherPaymentType", required = false) Integer otherPaymentType,
#RequestParam(value = "otherPaymentTransactionId", required = false) String otherPaymentTransactionId,
#RequestParam(value = "discountPercentorAmount", required = false) String discountPercentorAmount,
#RequestParam(value = "discountId", required = false) String discountId) throws AppException.RequestFieldError, AppException.CollectionAmountMoreThanOutstanding {
//method implementation
}

A Set or HashSet is a java concept. There is no such thing as a Set from the HTTP perspective, and there is no such thing as a Set in Postman. So from Postman, you need to send the invoice_ids in a format that Spring's parsing library can convert to a HashSet. As #Michael pointed out in the comments, one way to do this is to comma separate the invoice_ids like this: invoice_id=id1,id2,id3. When Spring processes this request, it will see that you are expecting data in the form of a HashSet, so it will attempt to convert id1,id2,id3 into a HashSet<Integer>, which it knows how to do automatically.
Side note: Unless you specifically need a HashSet, it is considered good practice to declare your type using the interface instead of an implementing subclass. So in this situation I would recommend changing your method signature to accept a Set<Integer> instead of a HashSet<Integer>

Related

Check variable run time Type in flutter with conditions like "123" is present as a String but is a int so how can i check this?

I have to check runtime Type of value for this I am using :-
for Example:-
String a = "abc";
int b = 123;
var c = "123"; //this is int value but because of this double quotes is represent as a String
a.runtimeType == String //true
b.runtimeType == int // true
c.runtimeType == String //true
c.runtimeType == int //false
a = "abc" // okay
b = 123 //okay
c = "123" //is issue
now I have to call a api with only String body in this case :-
this c is called the API because is String but i know this is a int value which is present as a String, so I have to stop this.
How can I check this??
when I am using try catch so my program is stopped because of FormatException error.
Note:- I don't know the real value of C, may be its "123" or "65dev" or "strjf" this value is changed every time.
and if i am parsing this in int to this return an error in many case.
Ok i understood that you want to pass "123" by checking and if it is int you are passing it , My question is what you will do if it is "123fe" you are going to pass as string? or you will pass nothing.
I don't know how you're passing it to API but if you wanna pass integer value from string quoted variable, you can parse/convert to integer like this.
int.parse(c);
either you can pass it directly or you can store in another variable and pass that variable.
Alternatively if you've int value and to have to pass it as a string, simply parse like this
integerValue.toString();
according to your code
b.toString();
Edit
String a = '20';
String b = 'a20';
try{
int check = int.parse(a);
//call your api inside try then inside if
if(check.runtimeType == int){
print('parsed $check');
}
}
catch(e){
print('not parsed ');
//handle your error
throw(e);
}
This will definitely help you!
String name = "5Syed8Ibrahim";
final RegExp nameRegExp = RegExp(r'^[a-zA-Z ][a-zA-Z ]*[a-zA-Z ]$');
print(nameRegExp.hasMatch(name));
//output false
name = "syed ibrahim";
print(nameRegExp.hasMatch(name));
//output true
Just check the output and based on that boolean value invoke api call
I hope it will done the work

URLComponent percentEncodedPath - Is value valid before setting it

Trying to pass a string to URLComponents's percentEncodedPath crashes the app if the string is not a valid percent encoded path.
Is there a way to tell if the string is a valid percent encoded path before I set it? I am having trouble figuring this one out.
var urlComponents = URLComponents()
urlComponents.percentEncodedPath = "/some failing path/" // <- This crashes
You can do something like this:
extension String {
var isPercentEncoded: Bool {
let decoded = self.removingPercentEncoding
return decoded != nil && decoded != self
}
}
The rationale:
If the String is URL-encoded, removingPercentEncoding will decode it, and hence decoded will be different from self
If the String contains a percent (but is not URL-Encoded), removingPercentEncoding will fail, and hence decoded will be nil
Otherwise string will remain unmodified
Example:
let failingPath = "/some failing path/"
let succeedingPath = "%2Fsome+succeeding+path%2F"
let doubleEncoded = "%252Fsome%2Bfailing%2Bpath%252F"
let withPercent = "a%b"
failingPath.isPercentEncoded // returns false
succeedingPath.isPercentEncoded // returns true
doubleEncoded.isPercentEncoded // returns true
withPercent.isPercentEncoded // returns false
One disadvantage is that you are on a mercy of removingPercentEncoding and also this is a "mechanical" interpretation of the string, which may not take into account the implementation intent. For example a%20b will be interpreted as URL-encoded. But what if your app expects that string as a decoded one (and so it needs to be encoded further)?

converting String Type to phoneNumber

I have a string that is actually a phone number. This string is stored as variable PhoneNumber in my database.
I want to convert this to a type phoneNumber() and then format it using the PhoneNumberKit and then put it back into a label as a string
How should i do this?
Convert the string to integer and then convert the string to phoneNumber struct present in phone number kit?
or is there anyway of simply passing the string itself to format function in phone Numberkit?
I have not written a full code yet. So this is what i have in my code:
func formatTOPhoneNumber(){
// userNumber is the string that is phone number
var userNumber = User.shared.phoneNumber
var num = (userNumber! as NSString).integerValue
}
The PhoneNumberKit library works with strings. Use the parse method to convert it into a PhoneNumber object. Then use the format function to convert it into a string:
var formattedString = ""
do {
let phoneNumberKit = PhoneNumberKit()
let phoneNumber = try phoneNumberKit.parse(userNumber)
formattedString = phoneNumberKit.format(phoneNumber, toType: .e164)
}
catch {
print("Generic parser error")
}
Try reading the documentation before asking a question, you might attract some downvotes when you haven't done any prior research.

How do I tell swagger that parameter dataType is an array of objects?

I am annotating action method with #ApiImplicitParams
new ApiImplicitParam(
dataType = "Array[Model]", // <- What should I write here?
paramType = "body",
name = "body",
required = true,
allowMultiple = false,
value = "The JSON array of messages to be logged.")
[LModel does not work as well.
You should be able to use dataTypeClass field, instead of dataType.
new ApiImplicitParam(
dataTypeClass = classOf[Array[Model]],
paramType = "body",
name = "body",
required = true,
value = "The JSON array of messages to be logged.")

cannot convert a dictionary's int-value to String in swift

I have a webService, which is returning some data. here is the link http://portal.pfs-ltd.org/LoginService1114?deviceid=89A04BDB-53AA-4441-9AFD-287729ACFE7F&imeino=12345678&SerialNo=f7pln20mfpfl&username=TST0002&password=TST0002&type=agent&status=Login&flag=True&Brand=Ipad&SimSerialNumber=&TelePhoneNumber=&Model=0&InstalledVersion=1.0
I'm unable to convert the value(int) for a key to a var of type String
var ApplicationVersion: String = dict["ApplicationVersion"] as String
as is for typecasting, not converting. You need to make a string from the integer value:
let version = dict["ApplicationVersion"] as Int
let applicationVersion = "\(version)"
Note that this performs no safety checking or validation.
Your json is not a dictionary, but a nested array.
let json: [AnyObject] = ...
let dict = json[0][0] as NSDictionary
var applicationVersion = String(dict["ApplicationVersion"] as Int)