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.
Related
In my postman mock server I would like to use data from the request. Is this possible? I can't seem to find any reference to this scenario.
For example, my request includes a documentId value. I would like to capture that value and use it in the response.
Thanks.
Postman supports capture of URL path parameters for use in the response body, so e.g. if your example has https://my.example.com/v1/users/{{user_id}} in the URL, then you can use {{user_id}} in the response.
That's about as far as it goes though. You can't at present use data from query parameters, headers or the request body in your responses.
If you need to use other types of request data in your mock responses, you might want to check out MockLab. I've written up a detailed comparison of Postman mock servers and MockLab including a specific on dynamic responses and request data.
Using Postman, when I make a PUT request to an endpoint which returns a 204 with content, Postman is unable to parse the response, and my collection runner stops that iteration, indicating that an error has occurred.
When run outside of the runner, Postman displays the following:
Other people have also had this problem
Unfortunately I cannot fix the non-standard endpoint. Is there a workaround that will let Postman continue without throwing an error, especially when using the collection runner?
The 204 (204 NO CONTENT) response from the server means that the server processed your request successfully and a response is not needed.
More here: https://httpstatuses.com/204
Actually as much as I know, if the server is sending a 204 with a payload response, the endpoint is not developed as it should.
This would be the main reason Postman is not showing a response payload. You will only be able to read response headers.
So if you send a PUT request, and only receive headers, it means everything is ok. If you spect data the server should be responding with a 200 code.
Now, said this, if postman is telling you that “it could not get any response” it means basically the server is not responding any thing. Now try to increase the timeout in the postman settings. It’s very probable that the server is taking to much time. Check outside the runner how much time it’s taking to response.
I hope this helps you.
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)