Understand HTTP Status Code and Fix Common Errors in REST API


Logo REST APIAny time you send HTTP Request according to HTTP Standard using tools like ZappySys REST API Drivers or SSIS Connectors you may receive web response along with HTTP Status Codes. Dealing with REST / SOAP API calls you may face errors and you may find it challenging to understand generic error message from server along with some Numeric value (Called HTTP Status Code).  In this article we will try to decode certain status codes and discuss various way to solve it.

Example of HTTP Error and Status Code

Before we dive deep on this topic of HTTP Errors Status Codes let’s check an example HTTP Error with Status Code 400 (BadRequest). As you notice we passed some bad data in Request Body to produce this error. As you may see in below screenshot there are few parts we need to configure to call REST API request.

  1. URL  (and optionally parameters in URL)
  2. Request Method  (i.e. GET / POST)
  3. Request Body (Optional – Not needed for GET request but for POST /PUT API requests you may need to pass)
  4. Request Content Type ( Optional – i.e. application/xml . This is not needed for GET Request)
  5. Request Headers (Optional – Many tools generate default headers behind the scene. In our example we passed only two headers but see how many more added automatically in below Raw View obtained by tools like Fiddler )

Sample Request:

Sample Response:

Here is a screenshot of SSIS REST API Task which can be used for calling XML / JSON API to automate ETL workflows.

Example of Calling REST API which returns HTTP Error Status Code 400 (Bad Request) - Invalid POST Body / Syntax Error

Example of Calling REST API which returns HTTP Error Status Code 400 (Bad Request) – Invalid POST Body / Syntax Error

Common HTTP Error Status Codes / Reasons / Possible Fix

Status codes can be anywhere between 100 to 505. Most of them documented here also here (RFC2616 – HTTP / 1.1). Out of these status codes we will discuss few most common ones in this article and how to fix them.

Microsoft has also provided great explanation for most common HTTP Status Code here.

Status Code 400 – Bad Request

Possible Reason(s):

  • One or more URL Parameter is wrong
  • Request body may be wrong (For example: Either data is invalid in Body or Syntax is invalid – such as JSON Property name misspelled or missing closing brackets)

Possible Fix:

  • Make sure you passing URL Parameters correct way (Refer your API help file or some sample code)
  • For POST / PUT request check your Parameter names, Data format, JSON / XML syntax (Refer your API help file or some sample code)
  • If you want to validate JSON / XML Syntax use some FREE online validators for JSON  or XML

Status Code 401 – Unauthorized

Possible Reason(s):

  • You did not pass userid / password or session token
  • Your credentials are invalid or session token is expired
  • Some tool may not save Sensitive Information unless you set Save Sensitive Information option. For example in SSIS if you set PackageProtectionLevel= DoNotSaveSensitive then password will not be sent along with your API request.

Possible Fix:

  • Make sure you passing credentials either directly via HTTP Headers or URL Parameter
  • For ZappySys Products use HTTP or OAuth connection manager to pass credentials securely without worrying about manual HTTP Headers
  • If you configured PackageProtectionLevel= DoNotSaveSensitive in SSIS then Check this and this  article to learn how to pass sensitive information in SSIS at runtime / design time.

Status Code 404 – Page Not Found

Possible Reason(s):

  • You entered invalid URL
  • URL is intranet only and you trying to access from outside network

Possible Fix:

  • Check your URL and correct it so its not throwing 404 error when you test in Browser (It must return data or throw some other error – E.g. 400 – Bad request or 401-Unauthorized)

Status Code 405 – Method Is Not Allowed

Possible Reason(s):

  • You selected invalid Request Method (see #2 in above screenshot)

Possible Fix:

  • Select correct Request Method. Refer to your API see it supports the method you selected. For example You selected POSt method to update some data but your API is expecting PUT method rather than POST.

Status Code 411 – Length Required

Possible Reason(s):

  • You selected request method as POST or PUT but did not pass any body

Possible Fix:

  • Make sure you supply some value in the Request Body field as per API specification. See #4 in the screenshot above.

Status Code 415 – Media Type Is Not Supported

Possible Reason(s):

  • You did not pass correct Content-Type Header or your API was expecting different content-type setting than what you passed.

Possible Fix:

  • Pass HTTP Header Content-Type with correct value (Check API documentation). Typically for JSON API its application/json  and for XML SOAP API its text/xml
  • If you are using Zappysys REST API Drivers or SSIS Connectors you will see Content Type Dropdown on UI see #4 in above screenshot. Change it to correct setting. If your content type not in the dropdown then you can set direct HTTP Header
    Example => Content-Type: application/json  see #5 in screenshot.

Debugging HTTP Web Requests

If you want to make sure what’s being passed to web server you can use this tool (Fiddler) and debug your web requests. It will show you all parts of your HTTP Request / Response (i.e.  Headers, Body, URL Parameters)

Posted in ODBC Drivers, ODBC PowerPack, REST API, SSIS PowerPack, SSIS REST API Task and tagged , , .