Writing JSON to API Produces HTTP 406 Response Code

To create or update ThousandEyes configurations via the ThousandEyes API, users send data via the HTTP POST or PUT methods. When sending an HTTP POST or PUT commands to the ThousandEyes API, if you receive an HTTP response code of "406 (Not Acceptable)" or "400 (Bad Request)", please check the following:

  • Verify that your JSON data is valid JSON

    Check that you have specified valid JSON using an online JSON verification tool, such as JSONLintarrow-up-right.

  • Verify all required fields are sent for your test and action (creation or update) and no fields specific to a different test or action

    For example, when creating an HTTP Server test, omitting the url field or using the Network test's server and port fields instead of url will result in a 400 status code. See the API documentationarrow-up-right for required fields for each API. Additionally, note that the API response may include extra information in the response body to help you understand why your request was considered invalid—review the response details for specific errors or missing fields.

  • Ensure that your JSON data uses double quotes rather than single quotes

    Only double quotes are valid for enclosing string values, per the JSON standard.arrow-up-right. Single quotes must not be used within the JSON data.

For example, when using the curl command from a Bash shell to send data, use single quotes to enclose the data for the -d command line switch, and double quotes within the data. The quoting should use the convention in the following example.

curl -i -XPUT https://api.thousandeyes.com/v7/tests/http-server/817 \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $BEARER_TOKEN" \
    -d '{"interval": 900, \
    "agents": [{"agentId": 117}],
    "testName": "Edited test name for API network test addition for www.thousandeyes.com"
    }'

Alternatively, some shell/operating system combinations may not accept single quotes around the JSON data specified with the -d switch. If you must enclose the JSON data in double quotes, you will need to escape the double quotes nested in the data. Nested double quotes may be escaped with a backslash. For example:

curl -i -XPUT https://api.thousandeyes.com/v7/tests/http-server/817 \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $BEARER_TOKEN" \
    -d "{\"interval\": 900, \
    \"agents\": [{\"agentId\": 117}],
    \"testName\": \"Edited test name for API network test addition for www.thousandeyes.com\"
    }" \

Note that if backslashes are used for the shell to continue across multiple lines, then you may need to escape your escape character by adding a backslash before the backslashes used to escape the double quotes! In this situation, you may instead wish to take advantage of curl's ability to take the JSON data from a file. See the curl man pagearrow-up-right for more information.

To see what JSON data is actually sent, you may use the curl command-line switch --trace-ascii <file>. This will write a copy of the data to a file along with the server response, allowing users to determine whether the data is being properly quoted. Here is the relevant portion of the output from the above command when run with the trace:

The last line indicates that the JSON data was sent with the correct quotation marks. If the JSON data appeared with no double quotes, then that would likely indicate that escaping the double quotes in the JSON payload on the command line is required.

You may verify that the JSON sent is valid JSON using an online JSON verification tool, such as JSONLintarrow-up-right.

Alternatively, you may wish to use an application for sending queries to RESTful APIs. A freeware example of such an application is Postmanarrow-up-right.

Last updated