Is possible to call another collection if the response is as 401 ?
I have a snippet , that is going to check if the response is a 200 e reads the response. But if is a 401 i need to repost the authentication , is it possible ?
You can call a different request within the same collection using postman.setNextRequest("request_name"); based on your 401 check.
Here is a demo of that feature.
Related
I have setup a mock server in Postman.
For a request X, I have added 2 examples (responses)
200 Success Response
400 Bad request
When I use x-mock-response-code I am able to get the appropriate response.
But when I dont use the x-mock-response-code, I am always getting 400 Bad Request. I am expecting 200 by default. But its not happening.
Do I need to add some thing to example response ? I tried to change example name as Default but no use..
If your example requests are identical, Postman will deterministically return the response for one of them. There is no concept of 'default' examples at this time.
If you want a particular response to be returned, make sure your example requests are not identical and only one example request matches the request you are sending.
Or use the x-mock-response-code header as you are already doing.
We have an API endpoint we are building through API Gateway that can have two possible successful responses: 200 OK if all of the requested data is returned, and a 206 Partial Content if the data is paginated and additional requests are required to fetch all the data.
I found at https://aws.amazon.com/blogs/compute/amazon-api-gateway-mapping-improvements/ that Amazon now allows multiple 2XX responses to be defined, but I cannot attribute both responses to a success in the Integration Response configuration. Right now we have 200 set as the default, but it appears as if we have to specify a Lambda Error Regex for the 206 mapping.
Does this basically mean that I have to fail the 206 with an error message and then use the regex to determine if that message is being sent, and then basically just treat it like a success? Or how can I properly return either a 200 or a 206 as a successful response?
The endpoint that AWS will hit on our server will properly return a 200 or a 206, but when AWS responds to the client it is currently only sending a 200.
Does this basically mean that I have to fail the 206 with an error
message and then use the regex to determine if that message is being
sent, and then basically just treat it like a success?
Yes, that is correct. Quirky, wrong, but it works.
The API gateway doesnt provide sending multiple success 2xx response. Below are some options
Make your lambda or backend application fail and return some json response. Create a regex in integration response and map the response to a particular error code. The matching of regex is done on errorMessage field which can be only get once you throw error from lambda.
Utilise reponse overriding. You can create a default mapping which matches all or some responses and then in the mapping template you can override the status code to whatever you want to send back.
Eg:-
Lambda returns
def handler(event, context):
x = {
"message": "partial"
}
return x
API gateway integration response
Lambda error regex: -
Method response status: 200
Default mapping: Yes
Mapping template:
#set($inputRoot = $input.path('$'))
$input.json("$")
#if($inputRoot.toString().contains("partial"))
#set($context.responseOverride.status = 206)
#end
I am testing an API with postman using simple GET, POST, and PUT requests. I have only 2 variables in the header each time (content-type and user) and am using a simple raw json script in the body when I run POST. Currently I only get 2-3 HTTP status response codes, 200 OK for success and 400 (bad request if I don't have the body info) and 404 if the URL is not correct. But I need to test multiple HTTP requests (201 Created, 202 Accepted, etc..) and I can't figure out how to trigger a specific response code. Using 201 as an example, I am using a test script like such: tests["Status code is 201 Created"] = responseCode.code === 201; Other than that, what do I need to do to trigger that specific response code? The HTTP/Semantics and Content doc says, the following but it doesn't make sense to me;
HTTP/1.1 Semantics and Content
The 201 (Created) status code indicates that the request has been
fulfilled and has resulted in one or more new resources being
created. The primary resource created by the request is identified by either a Location header field in the response or, if no Location
field is received, by the effective request URI.
The 201 response payload typically describes and links to the
resource(s) created. See Section 7.2 for a discussion of the meaning
and purpose of validator header fields, such as ETag and
Last-Modified, in a 201 response.
describe('response', function() {
it('status must be OK', function() {
response.should.have.status(200);
response.should.not.be.empty;
});
});
You can chage this line ex. with: response.should.have.status(201);
I hope help you with the examples.
What HTTP status code should I return when a POST request is made to my RESTful API but the content in the POST field (let's say an XML) is invalid?
I would like to build a proper RESTful web service so I want to know.
I am now returning 405 when a HTTP method not supported by specific API is used, 200 when everything goes ok and 500 for all other errors (XML validation error etc).
Thank you.
I would respond with 400
400 Bad Request
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
That's what status code 422 is for.
I'm implementing an API. The API accepts/returns JSON content type.
Now, suppose that the data submitted by some POST request is not valid, like a missing attribute, or a duplication exists for the same data.
What is the standard HTML response code in that case?
The error lies on the client side, so you want to use a 4xx status code. I'd go with 400 - Bad Request:
The request could not be understood by
the server due to malformed syntax.
The client SHOULD NOT repeat the
request without modifications.
There are two answers:
If you have submitted a form, just return 200 - OK with HTML explaining why the object was not created.
If you have an API you should use the following
200 OK
When the request was OK and returned the proper data.
201 CREATED
The call was successful and the new object created.
400 BAD REQUEST
Invalid request URI
Invalid HTTP Header
Receiving an unsupported, nonstandard parameter
Receiving an invalid HTTP Message Body
401 UNAUTHORIZED
Authorization problems. E.g. wrong API key, etc.
403 FORBIDDEN
Properly authorized, but not allowed.
404 NOT FOUND
The resource does not exist (e.g. on Read or Update)
405 METHOD NOT ALLOWED
Use in situations that a given REST method is not allowed. E.g. a POST on a single resource, or a DELETE on the entire collection of resources.
409 CONFLICT
When an update fails, send "Conflict" to allow the client side to resolve the conflict themselves and retry.
500 INTERNAL SERVER ERROR
Internal error. This is the default code that is used for all unrecognized errors.
501 NOT IMPLEMENTED
Use for expected, but not yet implemented features.
The closest i can find would be 400 Bad Request.
As Ariejan said you should base your API in the HTTP codes already defined. If you want to send a error message the best way should be not use the HTTP message, but better include the message in the response body, JSON formatted.
422 Unprocessable Entity (see RFC 4918, Section 11.2)