locked
POSTing request to builtin Website REST API Fails using Invoke-WebRequest RRS feed

  • Question

  • I'm trying to add new data entries to an existing REST API table using powershell's Invoke-WebRequest with the POST method. 

    Just prior to doing the POST request I have already sent a GET inquiry request to the same REST API using Invoke-WebRequest and it returns my data just fine and as expected.

    So when I call the function Add-Employee to do the POST I also pass with it the id number just received back from the previous GET request mentioned above, along with some other parameter data. The most important parameter passed to the Add-Employee function is a hashtable built from a line read from an input file. Once the hashtable ($empUpdateData) is returned then I try to convert it to a JSON string using this code:

    $reqBody =  ($empUpdateData | ConvertTo-Json -Depth 3).ToString()

    and when I look at the value of $reqBody, after writing it to the screen, it appears to be OK like this:

    {
        "lastName":  "Dummy",
        "phoneNumber":  "636-922-9999",
        "quickEducation":  "Bachelors",
        "slug":  "",
        "middleInitial":  "D",
        "bio":  "nothing",
        "firstName":  "Dum",
        "title":  "Adjunct Faculty",
        "type1":  "adjunct",
        "prefix":  "",
        "fax":  "999-999-9999",
        "building":  " ADM",
        "room":  "4123",
        "headLine":  "",
        "pagetitle":  "",
        "nickName":  "Dummy",
        "email":  "ddummy@stchas.edu",
        "education":  "Bachelors"
    }

    And I try to use it as is then along with this url:

    https://dotcmsstaging.stchas.edu:443/api/content/publish/1

    and with a headers object that looks like this:

    {
        "Authorization":  "Basic ....................................",
        "Content-Type":  "application/json",
        "Accept":  "text/plain"
    }

    with this request line:

    $response = Invoke-WebRequest -URI $url -Method Post -Headers $headers -Body $reqBody

    which always currently gives me a status code of 500 Internal Server Error

    one of the website support people that I've talked to said  that on the server he found the following error information:

    [13/12/17 23:31:54:764 CST] ERROR rest.ContentResource: Error processing JSON for Stream com.dotcms.repackage.org.codehaus.jettison.json.JSONException: A JSONObject text must begin with '{' at character 1 of @'{     "lastName":  "Dummy",     "phoneNumber":  "636-922-9999",     "quickEducation":  "Bachelors",     "slug":  "",     "middleInitial":  "D",     "bio":  "nothing",     "firstName":  "Dum",     "title":  "Adjunct Faculty",     "type1":  "adjunct",     "prefix":  "",     "fax":  "999-999-9999",     "building":  " ADM",     "room":  "4123",     "headLine":  "",     "pagetitle":  "",     "nickName":  "Dummy",     "email":  "ddummy@stchas.edu",     "education":  "Bachelors" }'@

    It makes sense that something is wrong with the JSON, but I'm not sure where it is I'm going wrong then with either the hashtable creation, and building of it, or in trying to convert it to a json format.

    I would appreciate any help or suggestions on this problem?

    thanks.

    Henry


    Thursday, December 14, 2017 6:47 PM

Answers