简体   繁体   中英

Does HTTP Status Code 500 means that the rest error code are not occuring

I am trying to update an object via REST API with an HTTP PUT request. I was getting different errors like 400 but now after amending my code and headers I am getting 500 which means that

 Internal Error 500 : The server encountered an unexpected 
 condition which prevented it from fulfilling the request. 

Does this mean the following errors I don't have to worry about, meaning the operation is authorized, method is not forbidden etc etc

  • Unauthorized 401
  • PaymentRequired 402
  • Forbidden 403
  • Not found 404

ps I am using JSON.

String json = "my json string with escape characters"
String st= "username:password";
byte[] encoded = Base64.encodeBase64(st.getBytes());           
String credentials = new String(encoded);
String url= "http://localhost:10108/grc/api/contents/20081";

URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();

con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept","application/json");
con.setRequestProperty  ("Authorization", "Basic " + credentials);  

con.setRequestMethod("PUT");

JSONObject jsonObject = (JSONObject)new JSONParser().parse(json);

OutputStream os = con.getOutputStream();
os.write(jsonObject.toJSONString().getBytes());
os.flush();

int responseCode = con.getResponseCode();

My original JSON is following

{
    "name": "Decommisioned CBU One",
    "id": "2116",   
    "description": "Decommisioned CBU One",
    "parentFolderId": "2115",
    "fields": {
        "field": [{
            "dataType": "ID_TYPE",
            "id": "29",
            "name": "Resource ID",
            "value": "2116"
        }, {
            "dataType": "STRING_TYPE",
            "id": "63",
            "name": "Comment"
        }, {
            "dataType": "INTEGER_TYPE",
            "id": "60",
            "name": "Created By",
            "value": 6
        }, {
            "dataType": "DATE_TYPE",
            "id": "59",
            "name": "Creation Date",
            "value": "2015-02-04T20:23:26.000+05:00"
        }, {
            "dataType": "STRING_TYPE",
            "id": "57",
            "name": "Description",
            "value": "Decommisioned CBU One"
        }, {
            "dataType": "DATE_TYPE",
            "id": "61",
            "name": "Last Modification Date",
            "value": "2015-02-04T20:23:26.000+05:00"
        }, {
            "dataType": "INTEGER_TYPE",
            "id": "62",
            "name": "Last Modified By",
            "value": 6
        }, {
            "dataType": "STRING_TYPE",
            "id": "58",
            "name": "Location",
            "value": "/_op_sox/Project/Default/BusinessEntity/Emirates NBD PJSC/Emirates NBD Bank/Wholesale Banking/(Decommissioned) Corporate Banking/Decommisioned CBU One/Decommisioned CBU One.txt"
        }, {
            "dataType": "STRING_TYPE",
            "id": "56",
            "name": "Name",
            "value": "Decommisioned CBU One.txt"
        }, {
            "dataType": "STRING_TYPE",
            "id": "66",
            "name": "Orphan"
        }, {
            "dataType": "STRING_TYPE",
            "id": "125",
            "name": "OPSS-BusEnt:Business Entity Chart",
            "value": "${\"labelKey\" : \"label.hierarchyDiagram.url\",   \"path\" : \"/visualization/VizRenderer.jsp\",   \"modes\" : [\"view\", \"edit\"],\n     \"parameters\" :{\"oid\" : \"$objectId\",\"viz\" : \"EntityHierarchy\"},\n     \"popUp\" : {       \"windowAttributes\" : \"height=800,width=1000,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,name=_blank\"\n     }\n     }"
        }, {
            "dataType": "STRING_TYPE",
            "id": "124",
            "name": "OPSS-BusEnt:Compliance Owner"
        }, {
            "dataType": "ENUM_TYPE",
            "id": "121",
            "name": "OPSS-BusEnt:Entity Type",
            "enumValue": {
                "id": "229",
                "name": "Business",
                "localizedLabel": "Business",
                "index": 2,
                "hidden": false
            }
        }, {
            "dataType": "STRING_TYPE",
            "id": "123",
            "name": "OPSS-BusEnt:Executive Owner"
        }, {
            "dataType": "ENUM_TYPE",
            "id": "133",
            "name": "OPSS-BusEnt:In RCSA Scope",
            "enumValue": {
                "id": "227",
                "name": "No",
                "localizedLabel": "No",
                "index": 2,
                "hidden": false
            }
        }, {
            "dataType": "MULTI_VALUE_ENUM",
            "id": "122",
            "name": "OPSS-BusEnt:In Scope"
        }, {
            "dataType": "STRING_TYPE",
            "id": "126",
            "name": "OPSS-BusEnt:Logo URL"
        }, {
            "dataType": "CURRENCY_TYPE",
            "id": "132",
            "name": "OPSS-BusEnt:Risk Appetite",
            "baseAmount": 0.0,
            "localAmount": 0.0
        }]
    },
    "typeDefinitionId": "6",
    "primaryParentId": "2112"
}

after removing linbreaks and using escape sequence, I am left with the follwowing string which I am using as my JSON

{ \"name\": \"Decommisioned CBU One\", \"id\": \"2116\", \"description\": \"Decommisioned CBU One\", \"parentFolderId\": \"2115\", \"fields\": { \"field\": [{ \"dataType\": \"ID_TYPE\", \"id\": \"29\", \"name\": \"Resource ID\", \"value\": \"2116\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"63\", \"name\": \"Comment\" }, { \"dataType\": \"INTEGER_TYPE\", \"id\": \"60\", \"name\": \"Created By\", \"value\": 6 }, { \"dataType\": \"DATE_TYPE\", \"id\": \"59\", \"name\": \"Creation Date\", \"value\": \"2015-02-04T20:23:26.000+05:00\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"57\", \"name\": \"Description\", \"value\": \"Decommisioned CBU One\" }, { \"dataType\": \"DATE_TYPE\", \"id\": \"61\", \"name\": \"Last Modification Date\", \"value\": \"2015-02-04T20:23:26.000+05:00\" }, { \"dataType\": \"INTEGER_TYPE\", \"id\": \"62\", \"name\": \"Last Modified By\", \"value\": 6 }, { \"dataType\": \"STRING_TYPE\", \"id\": \"58\", \"name\": \"Location\", \"value\": \"/_op_sox/Project/Default/BusinessEntity/Emirates NBD PJSC/Emirates NBD Bank/Wholesale Banking/(Decommissioned) Corporate Banking/Decommisioned CBU One/Decommisioned CBU One.txt\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"56\", \"name\": \"Name\", \"value\": \"Decommisioned CBU One.txt\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"66\", \"name\": \"Orphan\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"125\", \"name\": \"OPSS-BusEnt:Business Entity Chart\", \"value\": \"${\\"labelKey\\" : \\"label.hierarchyDiagram.url\\", \\"path\\" : \\"/visualization/VizRenderer.jsp\\", \\"modes\\" : [\\"view\\", \\"edit\\"],\n \\"parameters\\" :{\\"oid\\" : \\"$objectId\\",\\"viz\\" : \\"EntityHierarchy\\"},\n \\"popUp\\" : { \\"windowAttributes\\" : \\"height=800,width=1000,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,name=_blank\\"\n }\n }\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"124\", \"name\": \"OPSS-BusEnt:Compliance Owner\" }, { \"dataType\": \"ENUM_TYPE\", \"id\": \"121\", \"name\": \"OPSS-BusEnt:Entity Type\", \"enumValue\": { \"id\": \"229\", \"name\": \"Business\", \"localizedLabel\": \"Business\", \"index\": 2, \"hidden\": false } }, { \"dataType\": \"STRING_TYPE\", \"id\": \"123\", \"name\": \"OPSS-BusEnt:Executive Owner\" }, { \"dataType\": \"ENUM_TYPE\", \"id\": \"133\", \"name\": \"OPSS-BusEnt:In RCSA Scope\", \"enumValue\": { \"id\": \"227\", \"name\": \"No\", \"localizedLabel\": \"No\", \"index\": 2, \"hidden\": false } }, { \"dataType\": \"MULTI_VALUE_ENUM\", \"id\": \"122\", \"name\": \"OPSS-BusEnt:In Scope\" }, { \"dataType\": \"STRING_TYPE\", \"id\": \"126\", \"name\": \"OPSS-BusEnt:Logo URL\" }, { \"dataType\": \"CURRENCY_TYPE\", \"id\": \"132\", \"name\": \"OPSS-BusEnt:Risk Appetite\", \"baseAmount\": 0.0, \"localAmount\": 0.0 }] }, \"typeDefinitionId\": \"6\", \"primaryParentId\": \"2112\" }

Unfortunately, HTTP status 500 is really just a catch-all for unexpected errors on the server. The error could be anywhere, even within the authentication code. So it's possible that the call is blowing up even before it would have issued a 401 response.

In a perfect world, 500 responses wouldn't happen because developers would catch all possible errors and return useful responses instead.

A service owner should be interested in and concerned about 500 errors, a client shouldn't be able to cause them (a determined client could use them to DOS the service). The best thing to do next is to contact the service owner and ask them to look in their logs for the cause of the error.

最终,由服务器决定要返回什么状态码,但是5xx状态码(与4xx状态码相反)的标准含义是服务器端发生了问题(例如服务器)执行无效的取消引用或其他严重错误),而4xx状态代码则表示正在发送的客户端/请求有问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM