You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a bit strange API to handle. POST to the endpoint usually returns 201 with created resource, but if resource already exists it will return 200 and an array with that specific resource. This means depending on status code I receive different payload - how this can be handled?
The text was updated successfully, but these errors were encountered:
Hi.
Can you elaborate on the difficulty? From your description it sounds like a switch on the status code should do. Without more context I'd suggest something like:
If you are using automarshalling then you need to switch it off with SetDoNotParseResponse() then roll your own base on the status code per @lfrestrepog suggestion.
@arvenil FYI, the upcoming Resty v3, by this PR #902, brings an ability to control result or error struct based on status code or any other logic.
For example, something like this can be done -
client:=resty.New()
deferclient.Close()
resultByStatusCodeMiddleware:=func(c*Client, res*Response) error {
// Ideally in Resty v3 response middlewares should check error// before doing any processing, I'm ignoring this since, per // below example, this middleware is the first one.// if res.Err != nil {// return nil// }switchres.StatusCode() {
case200:
res.Request.SetResult(&StructA{})
case201:
res.Request.SetResult(&StructB{})
default:
// you can set SetError too, any other logic
}
returnnil
}
client.SetResponseMiddlewares(
resultByStatusCodeMiddleware,
resty.AutoParseResponseMiddleware, // before this, the body is not read by Resty except on debug flowresty.SaveToFileResponseMiddleware, // See, Request.SetOutputFile
)
Hi,
I have a bit strange API to handle. POST to the endpoint usually returns 201 with created resource, but if resource already exists it will return 200 and an array with that specific resource. This means depending on status code I receive different payload - how this can be handled?
The text was updated successfully, but these errors were encountered: