1

I am implementing a RESTful API that calls a third party service that returns an XML response like so for a failure:

<Status>Failure</Status>
<ErrorContent>One or more do not exist</ErrorContent>
<ErrorNum>20</ErrorNum>

I want to return a 400 response to users of my API when a failure is returned from the third-party. What is the best way to do that? I could always check to see if Status is Success or Failure and return accordingly, but it seems like there should be a better way to do it.

1
  • Use a try..catch around the 3rd party call/response. When you get a failure, throw your own response with the appropriate code. If they are returning additional 'successes' that you don't want your app to forward to your users then do something with those just like you mentioned. Commented Sep 19, 2018 at 17:32

2 Answers 2

1

I don't think there is a better way to do that. The XML is proprietary of the service you are consuming, so i don't know anything "built-in" to handle that. You could create a new class with the responsability to handle this or implement a base abstract class that could be inherited by all your API controllers that consumes this type of XML.

    public abstract class myApiController : ControllerBase
{
    protected IActionResult Response(string xmlresponse, object responseObject)//your "responseobject", you could remove this and return OKResult instead of OKObjectResult
    {
        if (IsSuccessXML(xmlresponse))
            return new OkObjectResult(responseObject);

        return new BadRequestResult();
    }

    private bool IsSuccessXML(string yourxmlResponse)//you could use the xml object instead of string
    {
        //read your xml response and "treat the response accordingly", returning true or false; you could also use the response object to show the errors to the users consuming your api
        return false;
0

I would agree with the other answers. I'd encapsulate the call to the 3rd party service and return success or failure. Then depending on what you want to do in each case, return a 400 or 200 or whatever you needs are. I don't think there is any magic you can do to make a better process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.