Answered by:
call to api using postman

Question
-
User527076549 posted
Hi,
i am trying to access api using postman
i have two api, which uses different bearer token. but both are supposed to give same result. Both had some issue, so i found i needed to Set Accept-Language,
which resolves issues in api1 ...but it does not work at all in api2.
Any Idea?? Really appreciate your help.Thanks in advance
Saturday, April 18, 2020 6:59 PM
Answers
-
User-1330468790 posted
Hi amithashenoy,
amithashenoy
but conversion works well when I use API in localhost.the values are coming properly.Actually, the conversion is allowed as I mentioned so that we don't need to focus on the conversion.
Both of the below code will be working perfectly.
Casting:
int value; if (int.TryParse(line, out value)) { switch (value) { case (int)(EnumName.NegativeOne): Console.WriteLine("It is Negative One"); break; case (int)EnumName.PositiveOne: Console.WriteLine("It is Positive One"); break; case (int)EnumName.Zero: Console.WriteLine("It is Zero"); break; } }
Not Casting:
int value; if (int.TryParse(line, out value)) { if(Enum.IsDefined(typeof(EnumName), value)) { EnumName toBeCheck = (EnumName)value; switch (toBeCheck) { case EnumName.NegativeOne: Console.WriteLine("It is Negative One"); break; case EnumName.PositiveOne: Console.WriteLine("It is Positive One"); break; case EnumName.Zero: Console.WriteLine("It is Zero"); break; } } else { Console.WriteLine("Value is not defined in EnumName"); } }
amithashenoy
issue is only when I try to access API hosted in awsThe problem occurs after published so that we should target the problem from this aspect first.
There should be an error message when you called the API, which could be accessed from DevTool in browser. Could you please share the error message?
Moreover, it would be better to have the information about the .net/.net core framework version of the host machine.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 20, 2020 3:08 AM -
User527076549 posted
Hi,
Thanks all for your replies,
Found that..
V had givn Enum negative values.
value = coming from db as -2 or -1
and int.tryparse(value,out int result) -> this was failing when api directly called from postman- dont know why [api hosted on ubuntu]
so instead of conversion we are now directly taking that value from db
swicth(value)
{case "-2": do something;break;} this is working
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 20, 2020 6:59 AM
All replies
-
User475983607 posted
...but it does not work at all in api2.Can you explain clearly and openly what "does not work" means? Are you receiving an error? Can you share code that reproduces this issue?
Saturday, April 18, 2020 7:10 PM -
User527076549 posted
This is how api 1 looks [i cant share actual apis]
https://mysite/api/v1/public/sample/id/postmethodname/id api2
https://mysite/api/v1/sample/id/postmethodname/id -- when i call this api 1 - one of the method get called which check for enum value
switch(value)
{
case (int)(EnumName.NegativeOne): -(-this conversion is not working, So i set Accept-language --I am able to get "Istrue" value, so tried same with api2 as well but does not work).
Istrue=true;
break;
}
Saturday, April 18, 2020 7:25 PM -
User-1330468790 posted
Hi Amitha,
From my best guessing, your problem, as you described, is that you can not cast the Enumeration to int so that you set Accept-language which makes the casting working.
However, this setting is not feasible for API2 which contains a same switch block. This block is also bothered by this casting problem. Am I understanding right?
If so, the problem should not locate at the conversion. For example, if the "value" is of type int, the evaluation value after the case keyword should be in type int and the conversion should be working here.
Instead, you should check if there is anything wrong with passing the parameter "value". I suggest you set the breakpoints and debug to see what value passed from the query string and what the type of the parameter "value" is before doing the match.
Hope this can help you.
Best regards,
Sean
Sunday, April 19, 2020 8:48 AM -
User475983607 posted
The little bit of code you've shared is very questionable. First, an enum switch looks like this.
EnumName val = (EnumName)value; bool isTrue; switch (val) { case EnumName.NegativeOne: isTrue = true; break; case EnumName.Zero: break; case EnumName.PositiveOne: break; }
Secondly, since ultimately the result is bool you can probably remove the switch.
EnumName val = (EnumName)value; bool isTrue = val == EnumName.NegativeOne;
Lastly, this is a support forum. The community cannot provide assistance without the source code or sample code that reproduces the issue.
Sunday, April 19, 2020 11:21 AM -
User527076549 posted
Yes, what you understood is correct.but conversion works well when I use API in localhost.the values are coming properly.issue is only when I try to access API hosted in awsSunday, April 19, 2020 3:52 PM -
User475983607 posted
Yes, what you understood is correct.but conversion works well when I use API in localhost.the values are coming properly.issue is only when I try to access API hosted in awsUsually when code does not work as intended there is an unwanted results like an error message or an unexpected response. So far you have not clarified the actual unwanted results or the steps to reproduce the unwanted results. All the community can see is code that could fail a code review.
Sunday, April 19, 2020 5:58 PM -
User-1330468790 posted
Hi amithashenoy,
amithashenoy
but conversion works well when I use API in localhost.the values are coming properly.Actually, the conversion is allowed as I mentioned so that we don't need to focus on the conversion.
Both of the below code will be working perfectly.
Casting:
int value; if (int.TryParse(line, out value)) { switch (value) { case (int)(EnumName.NegativeOne): Console.WriteLine("It is Negative One"); break; case (int)EnumName.PositiveOne: Console.WriteLine("It is Positive One"); break; case (int)EnumName.Zero: Console.WriteLine("It is Zero"); break; } }
Not Casting:
int value; if (int.TryParse(line, out value)) { if(Enum.IsDefined(typeof(EnumName), value)) { EnumName toBeCheck = (EnumName)value; switch (toBeCheck) { case EnumName.NegativeOne: Console.WriteLine("It is Negative One"); break; case EnumName.PositiveOne: Console.WriteLine("It is Positive One"); break; case EnumName.Zero: Console.WriteLine("It is Zero"); break; } } else { Console.WriteLine("Value is not defined in EnumName"); } }
amithashenoy
issue is only when I try to access API hosted in awsThe problem occurs after published so that we should target the problem from this aspect first.
There should be an error message when you called the API, which could be accessed from DevTool in browser. Could you please share the error message?
Moreover, it would be better to have the information about the .net/.net core framework version of the host machine.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 20, 2020 3:08 AM -
User527076549 posted
Hi,
No its throwing any error msg... as since it never execute this conversion part...in result....bool value returns false. [if its return true...with respect to this...it will print proper msg]
It just missing one part i result.
As you mentioned yes v have to chek version of .net/core of host
Monday, April 20, 2020 4:16 AM -
User527076549 posted
Hi,
Thanks all for your replies,
Found that..
V had givn Enum negative values.
value = coming from db as -2 or -1
and int.tryparse(value,out int result) -> this was failing when api directly called from postman- dont know why [api hosted on ubuntu]
so instead of conversion we are now directly taking that value from db
swicth(value)
{case "-2": do something;break;} this is working
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 20, 2020 6:59 AM -
User-1330468790 posted
Hi amithashenoy,
Glad to hear that you have found how to solve the problem.
I suggest you dig more for the reason of this problem by modifying the TryParse() to Parse() so that you will be able to catch the exception and check the exact reason for the error.
Anyway, if you find previous answers does help, I suggest you could mark the answer. This will help other people who faces the same issue to find the right answer faster.
Best regards,
Sean
Monday, April 20, 2020 9:52 AM -
User527076549 posted
Yes, will be digging deep.for time being we are continuing like this...Thanks.Monday, April 20, 2020 10:00 AM