I need to write some method that can be called using REST ( using 'Post' ).
This method need to get two parameters
a- some enum
b- some int
Is it sending possible to send this enum parameters ? If it is so how can i do it ?
Please find a sample for the above scenario:
[WebInvoke(UriTemplate = "GetEnumValues/{id}")]
string GetEnumValues(MyEnum e, string id);
public enum MyEnum
{
Fail = 0,
Success = 1
}
Now the request looks as follows when performing the post from fiddler:
URL : http://localhost/Sample/Service1.svc/GetEnumValue/5
User-Agent: Fiddler
Content-Type: application/xml
Host: localhost
<MyEnum xmlns="http://schemas.datacontract.org/2004/07/XMLService">Success</MyEnum>
NOTE: The parameter id is mapped as string as any parameter that is part of the query string needs to be a string type and then u can cast it to an int in your service implementation.
If you want to send both Enum and Id as part of request body then see below:
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string GetEnumValuesWrapped(MyEnum e, int id);
Now your requset looks as follows:
URL:http://localhost/Sample/Service1.svc/GetEnumValuesWrapped
User-Agent: Fiddler
Content-Type: application/xml
Host: localhost
<GetEnumValuesWrapped xmlns="http://tempuri.org/"><MyEnum xmlns="http://schemas.datacontract.org/2004/07/XMLService">Success</MyEnum><int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">5</int></GetEnumValuesWrapped>
Related
Code:
Feature: GET API headers feature
Scenario: pass GetWatchList with headers
Given header x-apisignatures = '543aba07839'
And header ssotoken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ1bmlxdWUiOiJjYTM4MDAzZS0wYThiLTQ3YjktOWFjNS00YzMyN2MwNTUyMGYiLCJ1c2VyVHlwZSI6IlJJTHBlcnNvbiIsImF1dGhMZXZlbCI6IjIwIiwiZGV2aWNlSWQiOiJlZWIxOGRlOGRiNjE3MDg4MjViNTdjNGU5NDg1ZmFjYTU1MGE1OTBkY2Y1MjIzNzkyYzUwYjVhZWRjZGI5ZmUxNDQxZTFiMzhmZWI2NDFhZmUwNGI3NGY0NDA5OWMzZGQwZWI0OTRiZjgwMTYxOWYyNTAyNjI2YTJjZjdkMTZmZCIsImp0aSI6IjFmN2IwODkxLTJkMGUtNDBhZS04MWJiLWQwODVjY2NhOGYyZSIsImlhdCI6MTY2NDI3Njk0NH0.6Q7CPtQIN0uB1ZbVQBj5dshqioop3dJKEpla5DQS5K5qtRPw38SxTEJ1f1DJ_Ka_sgMp_fdh9EEABipTgtgMqg'
And header uniqueid = 'ca38003e-0a8b-47b9-9ac5-4c327c05520f'
And header x-page = 'Home'
When url 'https://jiocinemaqa-api.jio.ril.com/user/v1/watchlist?groups=[["Movie"],["Show"]]'
When method GET
Then status 200
* print response
Try this public API to test:
* url 'https://httpbin.org/anything'
* param groups = '[["Movie"],["Show"]]'
* method get
And you can verify in the response you see this:
"args": {
"groups": "[[\"Movie\"],[\"Show\"]]"
},
And also:
"url": "https://httpbin.org/anything?groups=[[\"Movie\"]%2C[\"Show\"]]"
This proves that Karate is sending the right thing. Keep in mind that your server may have a bug.
I am have some problems passing in the correct headers for my graphql endpoints
The use case in Postman:
call requestToken endpoint to obtain sessionToken value
requestToken response contains Key Value " and Token Value.
For subsequent calls, I set postman headers as:
Key = X_SESSION_TOKEN Value = Token Value
The user case in Karate
1st feature 'requestToken.feature' successfully calls and stores key + tokenValue
2nd feature successfully defines and prints the token value
here is my 2nd request:
Feature: version
Background:
* url 'http://api-dev.markq.com:5000/'
* def myFeature = call read('requestToken.feature')
* def authToken = myFeature.sessionToken
* configure headers = { 'X_SESSION_TOKEN': authToken , 'Content-Type': 'application/json' }
Scenario: get version
Given path 'query'
Given text query =
"""
query {
version
}
"""
And request { query: '#(query)' }
When method POST
Then status 200
And print authToken
And print response
I am not sure I send the headers right. Its coming back 200, but I keep getting a error 'token malformed' in the response message
Any suggestions? New at this, thanks!
Honestly this is hard to answer, a LOT depends on the specific server.
EDIT: most likely it is this change needed, explained here: https://github.com/intuit/karate#embedded-expressions
* configure headers = { 'X_SESSION_TOKEN': '#(authToken)' , 'Content-Type': 'application/json' }
2 things from experience:
should it be X-SESSION-TOKEN
add an Accept: 'application/json' header
And try to hardcode the headers before attempting call etc.
Here is an example that works for me:
* url 'https://graphqlzero.almansi.me/api'
* text query =
"""
{
user(id: 1) {
posts {
data {
id
title
}
}
}
}
"""
* request { query: '#(query)' }
* method post
* status 200
I have an endpoint who consumes Json with 2 attributes, like
{id='12344', data=byte_array}
so I've wrote a test
Feature: submitted request
Scenario: submitted request
* def convertToBytes =
"""
function(arg) {
var StreamUtils = Java.type('my.utils.StreamUtils');
// it reads stream and convert it to a byte array
return StreamUtils.getBytes(arg);
}
"""
Given url 'http://my-server/post'
And def image = convertToBytes(read('classpath:images/image_1.jpg'));
And request {id:1, data: "#(image)"}
When method POST
Then status 200
However is got an exception form karate without much details
ERROR com.intuit.karate - http request failed: [B cannot be cast to [Ljava.lang.Object;
Any hits how to submit byte arrays as a part of Json with karate?
I don't think you can do that. Either the whole request should be binary (byte-array) or you do a multi-part request, where binary is Base64 encoded. As far as I know you can't put binary inside JSON. There is something called Binary JSON though.
EDIT: after assuming that the byte[] has to be Base64 encoded:
Background:
* url demoBaseUrl
* def Base64 = Java.type('java.util.Base64')
Scenario: json with byte-array
Given path 'echo', 'binary'
And def encoded = Base64.encoder.encodeToString('hello'.bytes);
And request { message: 'hello', data: '#(encoded)' }
When method post
Then status 200
And def expected = Base64.encoder.encodeToString('world'.bytes);
And match response == { message: 'world', data: '#(expected)' }
I just added this test to the Karate demos, and it is working fine. Here is the commit.
I am new to elm,
I have a login api which returns a JWT token in its hedears
curl http://localhost:4000/api/login?email=bob#example&password=1234
response:
HTTP/1.1 200 OK
authorization: Bearer eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXyLp0aSI6ImefP2GOWEFYWM47ig2W6nrhw
x-expires: 1499255103
content-type: text/plain; charset=utf-8
success
now Im trying to write a function that will send request and return the token from the headers in elm
authUser =
Http.send "http://localhost:4000/api/login?email=bob#example&password=1234"
how do I do this in a simple way?
In order to extract a header from a response, you will have to use Http.request along with the expectStringResponse function, which includes the full response including headers.
The expectStringResponse function takes a Http.Response a value, so we can create a function that accepts a header name and a response, then returns Ok headerValue or Err msg depending on whether the header was found:
extractHeader : String -> Http.Response String -> Result String String
extractHeader name resp =
Dict.get name resp.headers
|> Result.fromMaybe ("header " ++ name ++ " not found")
This could be used by a request builder like so:
getHeader : String -> String -> Http.Request String
getHeader name url =
Http.request
{ method = "GET"
, headers = []
, url = url
, body = Http.emptyBody
, expect = Http.expectStringResponse (extractHeader name)
, timeout = Nothing
, withCredentials = False
}
Here is an example on ellie-app.com which returns the value of content-type as an example. You can substitute "authorization" for your purposes.
May I humbly suggest you look at my elm-jwt library, and the get function there?
Jwt.get token "/api/data" dataDecoder
|> Jwt.send DataResult
JWT tokens normally need to be sent as a Authorization header and this function helps you create a Request type that can be passed to Http.send or Jwt.send
I have a REST Controller as follows using Spring boot 1.3.5 and Java 8. This works perfect while testing with Advanced REST Client (ARC) Utility - send xml request and returns the expected xml response as shown right below this controller code.
#RestController
#EnableAutoConfiguration
#RequestMapping("/rateQuote")
public class RateQuoteController {
#Autowired
private RateQuotingService rateQuotingService;
#RequestMapping(method = RequestMethod.POST, consumes = {"application/json","application/xml"}, produces = {"application/json","application/xml"})
public ResponseEntity<RateQuoteDto> getRateQuote(#RequestBody RESTRequestDto request){
RateQuoteDto rateQuoteDto = rateQuotingService.getRateQuote(request.getStateCode(), request.getZipCode(), request.getClient(), request.getThreshold(),
benefit,request.getLob(), localEffectiveDate);
return (new ResponseEntity<>(rateQuoteDto, HttpStatus.OK));
}
}
Request with ARC-
url - http://localhost:8080/rateQuote
Method - POST
Headers -
Content-Type: application/xml
Accept: application/xml
Request Body -
<request>
<stateCode>NY</stateCode>
<zipCode>10005</zipCode>
<client>BMG</client>
<threshold>1000</threshold>
<lob>PDI</lob>
<benefitLevel>15000</benefitLevel>
<effectiveDate>2016-01-01</effectiveDate>
</request
Response -
<?xml version="1.0" encoding="UTF-8"?>
<rateQuote>
<status>Approve</status>
<message>Quote is successfully retrieved.</message>
<quote>
<threshold1 amount="1000">
<benefits>
<benefitLevel amount="15000">
<annualPremium>990.00</annualPremium>
<semiAnnualPremium>440.00</semiAnnualPremium>
<quaterlyPremium>200.00</quaterlyPremium>
<monthlyPremium>77.00</monthlyPremium>
</benefitLevel>
</benefits>
</threshold1>
</quote>
</rateQuote>
So far so good. However, I have an issue with response when call is made grammatically.And the issue is that the most of the response's nodes are null as shown below:
#Test
public void getQuote()throws Exception{
RestTemplate template = new RestTemplate();
//RESTRequestDto is JAXB decorated
RESTRequestDto body = new RESTRequestDto();
body.setStateCode("NY");
body.setZipCode("10005");
body.setClient("BMG");
body.setThreshold("1000");
body.setLob("PDI");
body.setBenefitLevel("15000");
body.setEffectiveDate(DateUtils.createDate("2016-01-01", "yyyy-MM-dd"));
RequestEntity<RESTRequestDto> request = RequestEntity.post(new URI("http://localhost:8080/rateQuote"))
.accept(MediaType.APPLICATION_XML).contentType(MediaType.APPLICATION_XML).body(body);
ResponseEntity<RateQuoteDto> response = template.exchange(request, RateQuoteDto.class);
RateQuoteDto rateQuote = response.getBody();
System.out.println(rateQuote);
}
Response -
RateQuoteDto{status=Approve, message=Quote is successfully retrieved., zipCode=null, clientId=null, quote=QuoteDto{threshold1=ThresholdDto{amount=1000, benefitLevelLst=null}}}
However, changing the Media Type to JSON works as shown below:
RequestEntity<RESTRequestDto> request = RequestEntity.post(new URI("http://localhost:8080/rateQuote"))
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).body(body);
Response -
RateQuoteDto{status=Approve, message=Quote is successfully retrieved., zipCode=10005, clientId=BMG, quote=QuoteDto{threshold1=ThresholdDto{amount=1000, benefitLevelLst=[BenefitDto{amount=15000, annualPremium=990.00, semiAnnualPremium=440.00, quaterlyPremium=200.00, monthlyPremium=77.00}]}}}
Observation -
As shown below, the ist benefitLevelLst is not bound with MediaType.XML returning null value show above but works just fine with MediaType.JSON. Any idea as to why?
public class ThresholdDto {
#XmlAttribute
private String amount;
#XmlElementWrapper(name = "benefits")
#XmlElement(name = "benefitLevel")
private List<BenefitDto> benefitLevelLst;
. . .
}