Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle response if a call to API returns 200 with struct A, or 201 with struct B? #761

Closed
arvenil opened this issue Dec 13, 2023 · 4 comments
Assignees

Comments

@arvenil
Copy link

arvenil commented Dec 13, 2023

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?

@lfrestrepog
Copy link

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:

	switch r.StatusCode() {
	case 200:
		return handleUpdatedCase(r)
	case 201:
		return handleCreatedCase(r)
	default:
		return handleError(r)
	}

@alexhung
Copy link

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.

@jeevatkm
Copy link
Member

jeevatkm commented Mar 2, 2024

@arvenil Please go with combined suggestions from @alexhung & @lfrestrepog.

Also, in v3, I plan to improve response handling; Resty will possibly have out-of-the-box handling in this scenario.

@jeevatkm jeevatkm added this to the v3.0.0 Milestone milestone Mar 2, 2024
@jeevatkm jeevatkm self-assigned this Nov 6, 2024
@jeevatkm
Copy link
Member

jeevatkm commented Nov 6, 2024

@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()
defer client.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
	// }

	switch res.StatusCode() {
	case 200:
		res.Request.SetResult(&StructA{})
	case 201:
		res.Request.SetResult(&StructB{})
	default:
		// you can set SetError too, any other logic
	}
	return nil
}

client.SetResponseMiddlewares(
	resultByStatusCodeMiddleware,
	resty.AutoParseResponseMiddleware, // before this, the body is not read by Resty except on debug flow
	resty.SaveToFileResponseMiddleware, // See, Request.SetOutputFile
)

@jeevatkm jeevatkm closed this as completed Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants