I'm using swashbuckle to document my web api project and I cannot add multiple responses for precondition failed cases, swagger only shows the last one.
/// <response code="200">OK</response>
/// <response code="401">Unauthorized</response>
/// <response code="400">BadRequest</response>
/// <response code="412">ErrorCode = 1... </response>
/// <response code="412">ErrorCode = 2... </response>
/// <response code="412">ErrorCode = 3... </response>
Swashbuckle generates something like:
{
...
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "BadRequest"
},
"401": {
"description": "Unauthorized"
},
"412": {
"description": "ErrorCode = 3..."
}
}
...
}
Where I would like to see:
{
...
"412": {
"description": "ErrorCode = 1..."
},
"412": {
"description": "ErrorCode = 2..."
},
"412": {
"description": "ErrorCode = 3..."
}
}
Any thoughts about it?
Thanks in advance
What you are trying to achieve is not possible with Swashbuckle because it is not compliant with the Open API specification (aka Swagger). The responses object contains a list of possible responses. Each response has a name and a response object. The name can be default or a HTTP status code. According to the spec of a responses object there can be only one response object per status code:
Any HTTP status code can be used as the property name (one property per HTTP status code). Describes the expected response for that HTTP status code.
That being said, I doubt whether it is correct to add additional error codes to a 412 response. A conditional request might not reach your server in case a proxy server already knows the conditions won't be matched.
Related
I want to use while controller to keep sending GET HTTP Request and only proceed to next POST HTTP Request when the GET response "model_name": "Model-Test-20220221-0001"
POST HTTP Request - paramter model_name = Model-Test-${variable}-${counter}
While Controller
GET HTTP Request. sample GET response at below
Json Extractor
POST HTTP Request only when While Controller found the exact model_name.
JSON Extractor:
Names of created variable: modelname
Json path expressions: I have tried this but failed
$..valid_model_list[?(#.model_name = ${Model-Test-${variable}-${counter}})]
This is sample HTTP Request response:
{
"message": "success",
"valid_model_list": [
{
"meta_data": {
"corpus_list": [
"test1"
],
"id": "0c36effa244b4f6596d10f9e675303e1",
"sample_rate": 16000,
"split_ratio": {
"test": 40,
"train": 60
},
"model_name": "Model-Test-20220221-0001",
"status": "ok"
},
{
"meta_data": {
"corpus_list": [
"test1"
],
"id": "0c36effa244b4f6596d10f9e675303e1",
"sample_rate": 16000,
"split_ratio": {
"test": 40,
"train": 60
},
"model_name": "Model-Test-20220221-0002",
"status": "ok"
}
}
]
}
}
We're "unsure" as well as we don't know what you're trying to achieve.
JSON Extractor can extract values from the response, it's not possible to use it to extract values from the request, moreover you should "know" what you're sending in the request and extracting values from the response is the essential part of the correlation process
If you're looking for a JSONPath expression extracting the model_name attribute value you're supposed to provide full JSON. There is a Deep Scan operator so you should be able to use something like:
$..model_name
but if there are more than 1 matches you might need to limit the output with Filter Operators
More information:
JsonPath Getting Started
API Testing With JMeter and the JSON Extractor
I have a problem with creating teams using the Microsoft Graph Api. I can get/create groups but when I try to get/create teams I get an error. I'm using postman and the group has owners and members, just as the documentation of MS, also has the permissitions it asks for groups. If somebody can help me, cause I look everywhere for a same error but no found it.
PUT https://graph.microsoft.com/v1.0/groups/{id}/team
Headers: Authorization: bearer token and content-type: json
Body is
{
"memberSettings": {
"allowCreateUpdateChannels": true
},
"messagingSettings": {
"allowUserEditMessages": true,
"allowUserDeleteMessages": true
},
"funSettings": {
"allowGiphy": true,
"giphyContentRating": "strict"
}
}
I always get the same error
{
"error": {
"code": "BadGateway",
"message": "Failed to execute backend request.",
"innerError": {
"request-id": "45eeba8a-9d35-45e8-b42e-c60da7a47dde",
"date": "2020-01-23T21:55:44"
}
}
}
According to the Graph API docs for this, you're not calling the correct endpoint to create a new Team. It should be
POST https://graph.microsoft.com/beta/teams
and a payload similar to
Content-Type: application/json
{
"template#odata.bind": "https://graph.microsoft.com/beta/teamsTemplates('standard')",
"displayName": "My Sample Team",
"description": "My Sample Team’s Description",
"owners#odata.bind": [
"https://graph.microsoft.com/beta/users('userId')"
]
}
Note that it's slightly different, as per the docs, whether you're using delegated versus application permissons.
I've used the Swashbuckle swagger Nuget package in a .net framework webapi.
I'm trying to annotate api responses. It's great for success responses, I just specify the type of my object model:
[SwaggerResponse((int)HttpStatusCode.OK, Type = typeof(WebAPI.Models.APIResponse), Description = "Successful operation.")]
If I return an error like this:
return this.Request.CreateErrorResponse(HttpStatusCode.NotFound, "Document not found.");
I get the following json / xml responses
{
"Message": "Document not found."
}
<Error>
<Message>Document not found.</Message>
</Error>
If there is an unhandled exception, I will get an http 500 with a json / xml response like this (Not, the exception details are only returned when executing the rest call from the local machine.:
{
"Message": "An error has occurred.",
"ExceptionMessage": "This is a test error",
"ExceptionType": "System.Exception",
"StackTrace": " at WebAPI.Controllers.MyController.Post(Guid id) in ..."
}
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>This is a test error</ExceptionMessage
<ExceptionType>System.Exception</ExceptionType>
<StackTrace> at WebAPI.Controllers.MyController.Post(Guid id) in ...</StackTrace>
</Error>
For these responses, how do I annotate them with Swashbuckle?
I've tried:
[SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(HttpResponseMessage), Description = "Error.")]
[SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(HttpResponseException), Description = "Error.")]
[SwaggerResponse((int)HttpStatusCode.InternalServerError, Type = typeof(Exception), Description = "Error.")]
In the model example section I either get the message "Object is not a primitive" as in the case of Exception and HttpResponse, or the example does not reflect what the response would actually look like. For example HttpResponseException looks like this in the swagger ui:
{
"Response": {},
"Message": "string",
"Data": {},
"InnerException": {},
"StackTrace": "string",
"HelpLink": "string",
"Source": "string",
"HResult": 0
}
<?xml version="1.0"?>
<HttpResponseException>
<!-- invalid XML -->
<Message>string</Message>
<Data>
<!-- additional elements allowed -->
</Data>
<!-- invalid XML -->
<StackTrace>string</StackTrace>
<HelpLink>string</HelpLink>
<Source>string</Source>
<HResult>1</HResult>
</HttpResponseException>
I am attempting some simple tests on the Google Speech API, and when my server makes a request to this url (below), I get the 404. that's an error response. Not sure why.
https://speech.googleapis.com/v1/speech:recognize?key=[MY_API_KEY]
The body of my request looks like this:
{
"config": {
"languageCode": "en-US",
"encoding": "LINEAR16",
"sampleRateHertz": 16000,
"enableWordTimeOffsets": true,
"speechContexts": [{
"phrases": ["Some", "Helpful", "Phrases"]
}]
},
"audio":{
"uri":"gs://mydomain.com/my_file.mp3"
}
}
And here is the response:
As you can see, that is a valid resource path, unless I'm totally mistaken about something (I'm sure I am): https://cloud.google.com/speech-to-text/docs/reference/rest/v1/speech/recognize
Update 1:, Whenever I try this with the Google API explorer tool, I get this quota exceeded message (even though I have not yet issued a successful request to the API).
{
"error": {
"code": 429,
"message": "Quota exceeded for quota metric 'speech.googleapis.com/default_requests' and limit 'DefaultRequestsPerMinutePerProject' of service 'speech.googleapis.com' for consumer '[MY_API_KEY]'.",
"status": "RESOURCE_EXHAUSTED",
"details": [
{
"#type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developer console API key",
"url": "https://console.developers.google.com/project/[my_project_id]/apiui/credential"
}
]
}
]
}
}
Update 2: Interestingly, I was able to get some 200 ok's using the Restlet client, but even in those cases, the response body is empty (see screenshot below)
I have made a test by using the exact URL and Body content you added to the post, however, I was able to execute the API call correctly.
I noticed that if I add some extra character to the URL, it fails with the same 400 error since it doesn't exist. I would suggest you to verify that the URL of your request doesn't contain a typo and that the client you use is executing the API call correctly. Also, ensure that your calling code is not encoding the url, which could cause issues given the colon : that appears in the url.
I recommend you to perform this test by using the Try this API tool directly or Restlet client which are the ones that I used to replicate this scenario.
I have problem with the ESPN API:
I have an API key, and sent a query to the address http://api.espn.com/v1/sports/news/1581816?region=GB, which included my API key in the parameters.
I received 403 forbidden response: with this JSON
{
"status": "error",
"code": 403,
"message" : "Account Inactive"
}
I am looking at http://developer.espn.com/docs/headlines#using-the-api and at the last line it says that i can make a GET request to this url and it should work, but it doesn't.
How can I receive a valid response from the ESPN servers?
I just test it and it works fine. That means your key is invalid, or you are not including it. Replace below and it will work.
http://api.espn.com/v1/sports/news/1581816?region=GB&apikey=<yourKey>
--
{
timestamp: "2013-10-13T15:33:49Z",
resultsOffset: 0,
status: "success",
resultsLimit: 10,
resultsCount: 1,
headlines: [
{ ... }
]
}