(Akka-HTTP) How to specify a custom reason? - akka-http

I am trying to respond with a custom reason code. For example:
404 Didn't find it anywhere!
or
400 Bad Request: missing parameter 'a'
How is this done with Akka HTTP?

Not sure if you mean a custom status code or a custom reason.
To return a 404 status code you can simply return:
HttpResponse(404)
To return a customized reason you can return:
HttpResponse().withStatus(StatusCodes.custom(404, "Didn't find it anywhere!", ""))

Related

Getting KeyError of missing `code` argument

I use authlib with MSAD and I am getting KeyError on line:
https://github.com/lepture/authlib/blob/master/authlib/integrations/flask_client/integration.py#L43
My question is if there has to be always code argument within request or IMHO this is bug and it should be checked by get() method same as state.
I use it with flask and after proper registration of MSAD client I try method authorize_access_token() and from here I get to the poit of code I mentioned above and get error.
Thanks for any advice.
Vit
Are you using debug mode of Flask? In production, this code will raise 400 Bad request error.

Making the same call twice in a row doesn't update the response

What I'm trying to do is hit a service twice in a row with a post of the same data. The idea here is to confirm that I can't have duplicate data, so I should get a 422 and a specific message in the response.
The test looked something like this:
When javaClient.createFoo(parameters)
And javaClient.createFoo(parameters)
Then status 422
And match $.message == "This is a duplicate."
It doesn't have a problem matching the status of the second call, but when I try to match the message (or any part of the response) it compares to the response from the first call.
I've tried making the second call a When, tried checking the status of the first call in between, but there must be something else that I'm missing.
Any ideas? Is there something special I need to do to clear the response?
Unless the method step is called, Karate does not do anything. The use of javaClient.createFoo() (whatever that is) suggests that there are some fundamental problems in your use and understanding of Karate.
I suggest keep it simple and just repeat the method post with the same payload and it should work as you expect.
Also please read this: https://stackoverflow.com/a/54126724/143475

Post method gets converted to GET after redirection

I have one POST call related to search.It is like I am sending some data as parameters to call and some in payload.after getting 302 it gets redirected.But the issue is once it gets redirected,POST call gets converted to GET call and payload is lost.As a result I am unable to get desired search result.Is there anything related to config that I might be missing??
Yes this is the correct behavior. Sounds like you need to disable automatic re-directs for this test, see configure. You can do:
* configure followRedirects = false
And then get the redirect location manually as follows:
* def location = responseHeaders['Location'][0]
Refer to this test for an example: redirect.feature

Karate API: JSON parsing error shown instead of wrong HTTP code returned

Given have the following list of steps
Given path 'verify'
And header x-api-key = apiKey
And header tenant-id = tenantId
And request a_json_object_with_invalid_user_id_to_verify
When method put
Then status 404
When the endpoint returned HTTP 200 (which is a bug that i need to fix) but with an invalid json response such as
{
"score" :
}
This exception is thrown
com.jayway.jsonpath.InvalidJsonException: net.minidev.json.parser.ParseException: Unexpected character (}) at position 15.
at com.jayway.jsonpath.spi.json.JsonSmartJsonProvider.parse(JsonSmartJsonProvider.java:64)
at com.jayway.jsonpath.internal.JsonContext.parse(JsonContext.java:82)
at com.jayway.jsonpath.JsonPath.parse(JsonPath.java:596)
at com.intuit.karate.JsonUtils.toJsonDoc(JsonUtils.java:84)
at com.intuit.karate.StepDefs.method(StepDefs.java:344)
I expect to see the test failed because of wrong status code 200 received of the expected 404. I would like to know if throwing this exception is correct in karate?
Thanks.
I apologize for mis-reading your question.
There is indeed a bug in Karate when handling malformed JSON responses.
We have opened an issue, you should be able to get a patch version very soon: https://github.com/intuit/karate/issues/259

Nest1.0: ConnectionStatus error handling

I have a question regarding to Nest1.0pr and the connection error handling. In the previous versions of Nest I was using IResponse.ConnectionStatus.Error. It seems to me that the property Error does not exist in the new version anymore. However in the documentation I found the following:
ConnectionStatus is the response as it was returned by
Elasticsearch.net. It's section on handling responses applies here as
well.
And in the very section the property Error is mentioned.
Error When a call succeeds but does not return a http status code of
200 this property will have details on the error. Read more about
error handling here
So is the recommended way to check whether the property Success is false?
TIA
This changed when NEST was refactored to use Elasticsearch.Net. Now when a request fails, you can try checking the IResponse.ConnectionStatus.OriginalException property, which will contain the actual Elasticsearch error.