1

I'm trying to make a POST request to my server. Everything was fine until I decided to convert my object to JSON. Here's my code:

$postParams = @{
  Login = "JonSnow66";
  Password = "LetItSnow";
  Email = "[email protected]";

  Name = "Jon Snow";
  Desc = "I know nothing";
  BirthDate = "1572 2 16";
  Img = Get-Content -Path ./PH_img.txt | Out-String;
  Type = "Admin";
}

Invoke-WebRequest -Uri http://localhost:3000/api/add/user -Method POST -Body (ConvertTo-Json $postParams -Compress)

Instead of returning regular JSON object like:

{
"Login": "JonSnow66"
...
}

It returns:

{{
    "Login":  "JonSnow66",
    "BirthDate":  "1572 2 16",
    "Desc":  "I know nothing",
    "Name":  "Jon Snow",
    "Type":  "Admin",
    "Password":  "LetItSnow",
    "Img":  "/9j/4<BASE64>/Z\r\n",
    "Email":  "[email protected]"
}: ""}

I'm just a powershell beginner.

0

1 Answer 1

1

I think you need to specify ContentType on Invoke-WebRequest to be 'application/json'. If you don't specify a content type and are performing a Post then I think the cmdlet assumes you are submitting a form by default, and that might explain the extra { } characters you are seeing in the result.

Here's the modified code:

Invoke-WebRequest -Uri 'http://localhost:3000/api/add/user' -Method POST  -ContentType 'application/json' -Body (ConvertTo-Json $postParams -Compress)
Sign up to request clarification or add additional context in comments.

1 Comment

I was literally in the middle of writing Python script for that, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.