Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException

I am testing a Web Core API and I am getting a 500 Internal Server error. My method on my controller is;

[Route("api/property")]
public class PropertyController : BaseSirController
{

    [HttpPost("{contractId}/saveproperty")]
    public IActionResult SaveProperty(int contractId, [FromBody] PropertyContractDto property)
    {
        if (property == null)
        {
            return BadRequest();
        }

        if (contractId == 0)
        {
            return NotFound();
        }

        return Ok();
        //return CreatedAtRoute("GetPropertyInspectionsForContract", new {contractId = contractId}, properties);
    }

and in my POwershell command I enter:

Invoke-RestMethod http://localhost:17972/api/property/7/saveproperty -Method POST -Body (@{UPRN="1244"; StreetName="The Street"; AddressLine2="ALine2"; AddressLine3="ALine3"; AddressLine4="ALine4"; Postcode="N1 7PL"; Latitude="23"; Longitude="77"; BlockUPRN="B1244"; PropertyNo="23"; BlockName="Wenlock Court"; Comments="blah blah blah"; PropertyTypeId="1"; NumberOfBathrooms="1"; NumberOfBedrooms="2"; NumberOfKitchens="1"; PropertyContractId="23"; PropertyType="Maisonette"} | ConvertTo-Json) -ContentType "application/json"

The error message I am getting is;

Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error.
At line:1 char:1
+ Invoke-RestMethod http://localhost:17972/api/property/7/saveproperty -Method POS ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I cannot work out how to fix this.

like image 204
arame3333 Avatar asked Jun 16 '17 16:06

arame3333


People also ask

What is the difference between invoke RestMethod and invoke-WebRequest?

Invoke-RestMethod - unlike Invoke-WebRequest - has deserialization built in: with a JSON response, it automatically parses the JSON text returned into a [pscustomobject] graph as if ConvertFrom-Json had been applied to it.

What is Uri in invoke-WebRequest?

Invoke-WebRequest sends a request to the URI (Uniform Resource Identifier) which is also called Endpoint and retrieves the data from the Web Page. It directly works with the URL or with the REST API because some websites allow modifying the content using only APIs.


1 Answers

I found the problem was in my Startup.cs, ConfigureServices method. If I put in services.AddMvcCore().AddJsonFormatters(); it works

like image 144
arame3333 Avatar answered Nov 08 '22 08:11

arame3333